The section 4.23 describes how to define a function. In this chapter we describe functions that give information about functions, and various utility functions used either when defining functions or calling functions.
‣ NameFunction ( func ) | ( operation ) |
returns the name of a function. For operations, this is the name used in their declaration. For functions, this is the variable name they were first assigned to. (For some internal functions, this might be a name different from the name that is documented.) If no such name exists, the string "unknown"
is returned.
gap> NameFunction(SylowSubgroup); "SylowSubgroup" gap> Blubberflutsch:=x->x;; gap> NameFunction(Blubberflutsch); "Blubberflutsch" gap> a:=Blubberflutsch;; gap> NameFunction(a); "Blubberflutsch" gap> NameFunction(x->x); "unknown" gap> NameFunction(NameFunction); "NameFunction"
‣ NumberArgumentsFunction ( func ) | ( operation ) |
returns the number of arguments the function func accepts. -1 is returned for all operations. For functions that use ...
or arg
to take a variable number of arguments, the number returned is -1 times the total number of parameters. For attributes, 1 is returned.
gap> NumberArgumentsFunction(function(a,b,c,d,e,f,g,h,i,j,k)return 1;end); 11 gap> NumberArgumentsFunction(Size); 1 gap> NumberArgumentsFunction(IsCollsCollsElms); 3 gap> NumberArgumentsFunction(Sum); -1 gap> NumberArgumentsFunction(function(a, x...) return 1; end); -2
‣ NamesLocalVariablesFunction ( func ) | ( operation ) |
returns a mutable list of strings; the first entries are the names of the arguments of the function func, in the same order as they were entered in the definition of func, and the remaining ones are the local variables as given in the local
statement in func. (The number of arguments can be computed with NumberArgumentsFunction
(5.1-2).)
gap> NamesLocalVariablesFunction(function( a, b ) local c; return 1; end); [ "a", "b", "c" ] gap> NamesLocalVariablesFunction(function( arg ) local a; return 1; end); [ "arg", "a" ] gap> NamesLocalVariablesFunction( Size ); fail
‣ FilenameFunc ( func ) | ( function ) |
For a function func, FilenameFunc
returns either fail
or the absolute path of the file from which func has been read. The return value fail
occurs if func is a compiled function or an operation. For functions that have been entered interactively, the string "*stdin*"
is returned, see Section 9.5.
gap> FilenameFunc( LEN_LIST ); # a kernel function fail gap> FilenameFunc( Size ); # an operation fail gap> FilenameFunc( x -> x^2 ); # an interactively entered function "*stdin*" gap> meth:= ApplicableMethod( Size, [ Group( () ) ] );; gap> FilenameFunc( meth ); "... some path .../grpperm.gi"
‣ StartlineFunc ( func ) | ( function ) |
‣ EndlineFunc ( func ) | ( function ) |
Let func be a function. If FilenameFunc
(5.1-4) returns fail
for func then also StartlineFunc
returns fail
. If FilenameFunc
(5.1-4) returns a filename for func then StartlineFunc
returns the line number in this file where the definition of func starts.
EndlineFunc
behaves similarly and returns the line number in this file where the definition of func ends.
gap> meth:= ApplicableMethod( Size, [ Group( () ) ] );; gap> FilenameFunc( meth ); "... some path ... gap4r5/lib/grpperm.gi" gap> StartlineFunc( meth ); 487 gap> EndlineFunc( meth ); 487
‣ PageSource ( func ) | ( function ) |
This shows the file containing the source code of the function or method func in a pager (see Pager
(2.4-1)). The display starts at a line shortly before the code of func.
This function works if FilenameFunc(func)
returns the name of a proper file. In that case this filename and the position of the function definition are also printed. Otherwise the function indicates that the source is not available (for example this happens for functions which are implemented in the GAP C-kernel).
Usage examples:
met := ApplicableMethod(\^, [(1,2),2743527]); PageSource(met);
PageSource(Combinations);
ct:=CharacterTable(Group((1,2,3)));
met := ApplicableMethod(Size,[ct]); PageSource(met);
‣ CallFuncList ( func, args ) | ( operation ) |
returns the result, when calling function func with the arguments given in the list args, i.e. args is "unwrapped" so that args appears as several arguments to func.
gap> CallFuncList(\+, [6, 7]); 13 gap> #is equivalent to: gap> \+(6, 7); 13
A more useful application of CallFuncList
is for a function g
that is called in the body of a function f
with (a sublist of) the arguments of f
, where f
has been defined with a single formal argument arg
(see 4.23), as in the following code fragment.
f := function ( arg ) CallFuncList(g, arg); ... end;
In the body of f
the several arguments passed to f
become a list arg
. If g
were called instead via g( arg )
then g
would see a single list argument, so that g
would, in general, have to "unwrap" the passed list. The following (not particularly useful) example demonstrates both described possibilities for the call to g
.
gap> PrintNumberFromDigits := function ( arg ) > CallFuncList( Print, arg ); > Print( "\n" ); > end; function( arg... ) ... end gap> PrintNumberFromDigits( 1, 9, 7, 3, 2 ); 19732 gap> PrintDigits := function ( arg ) > Print( arg ); > Print( "\n" ); > end; function( arg... ) ... end gap> PrintDigits( 1, 9, 7, 3, 2 ); [ 1, 9, 7, 3, 2 ]
‣ CallWithTimeout ( timeout, func, ..... ) | ( function ) |
‣ CallWithTimeoutList ( timeout, func, arglist ) | ( function ) |
CallWithTimeout
and CallWithTimeoutList
support calling a function with a limit on the CPU time it can consume.
This functionality may not be available on all systems and you should check GAPInfo.TimeoutsSupported
(5.3-2) before using this functionality.
CallWithTimeout
is variadic. Its third and subsequent arguments, if any, are the arguments passed to func. CallWithTimeoutList
in contrast takes exactly three arguments, of which the third is a list (possibly empty) or arguments to pass to func.
If the call completes within the allotted time and returns a value res
, the result of CallWithTimeout[List]
is a length 2 list of the form [ true, res ]
.
If the call completes within the allotted time and returns no value, the result of CallWithTimeout[List]
is a list of length 1 containing the value true
.
If the call does not complete within the timeout, the result of CallWithTimeout[List]
is a list of length 1 containing the value false
In this case, just as if you had quit
from a break loop, there is some risk that internal data structures in GAP may have been left in an inconsistent state, and you should proceed with caution.
The timer is suspended during execution of a break loop and abandoned when you quit from a break loop.
Timeouts may not be nested. That is, during execution of CallWithTimeout(timeout,func,...)
, func (or functions it calls) may not call CallWithTimeout
or CallWithTimeoutList
. This restriction may be lifted on at least some systems in future releases. It is permitted to use CallWithTimeout
or CallWithTimeoutList
from within a break loop, even if a suspended timeout exists, although there is limit on the depth of such nesting.
The limit timeout is specified as a record. At present the following components are recognised nanoseconds
, microseconds
, milliseconds
, seconds
, minutes
, hours
, days
and weeks
. Any of these components which is present should be bound to a positive integer, rational or float and the times represented are totalled to give the actual timeout. As a shorthand, a single positive integers may be supplied, and is taken as a number of microseconds. Further components are permitted and ignored, to allow for future functionality.
The precision of the timeouts is not guaranteed, and there is a system dependent upper limit on the timeout which is typically about 8 years on 32 bit systems and about 30 billion years on 64 bit systems. Timeouts longer than this will be reduced to this limit. On Windows systems, timing is based on elapsed time, not CPU time because the necessary POSIX CPU timing API is not supported.
‣ GAPInfo.TimeoutsSupported | ( global variable ) |
tests whether this installation of GAP supports the timeout functionality of CallWithTimeout
(5.3-1) and related functions.
The following functions return fixed results (or just their own argument). They can be useful in places when the syntax requires a function, but actually no functionality is required. So ReturnTrue
(5.4-1) is often used as family predicate in InstallMethod
(78.2-1).
‣ ReturnTrue ( ... ) | ( function ) |
This function takes any number of arguments, and always returns true
.
gap> f:=ReturnTrue; function( arg... ) ... end gap> f(); true gap> f(42); true
‣ ReturnFalse ( ... ) | ( function ) |
This function takes any number of arguments, and always returns false
.
gap> f:=ReturnFalse; function( arg... ) ... end gap> f(); false gap> f("any_string"); false
‣ ReturnFail ( ... ) | ( function ) |
This function takes any number of arguments, and always returns fail
.
gap> oops:=ReturnFail; function( arg... ) ... end gap> oops(); fail gap> oops(-42); fail
‣ ReturnNothing ( ... ) | ( function ) |
This function takes any number of arguments, and always returns nothing.
gap> n:=ReturnNothing; function( object... ) ... end gap> n(); gap> n(-42);
‣ ReturnFirst ( ... ) | ( function ) |
This function takes one or more arguments, and always returns the first argument. IdFunc
(5.4-6) behaves similarly, but only accepts a single argument.
gap> f:=ReturnFirst; function( object... ) ... end gap> f(1); 1 gap> f(2,3,4); 2 gap> f(); Error, RETURN_FIRST requires one or more arguments
‣ IdFunc ( obj ) | ( function ) |
returns obj. ReturnFirst
(5.4-5) is similar, but accepts one or more arguments, returning only the first.
gap> id:=IdFunc; function( object ) ... end gap> id(42); 42 gap> f:=id(SymmetricGroup(3)); Sym( [ 1 .. 3 ] ) gap> s:=One(AutomorphismGroup(SymmetricGroup(3))); IdentityMapping( Sym( [ 1 .. 3 ] ) ) gap> f=s; false
Functions are GAP objects and thus have categories and a family.
‣ IsFunction ( obj ) | ( category ) |
is the category of functions.
gap> IsFunction(x->x^2); true gap> IsFunction(Factorial); true gap> f:=One(AutomorphismGroup(SymmetricGroup(3))); IdentityMapping( Sym( [ 1 .. 3 ] ) ) gap> IsFunction(f); false
‣ IsOperation ( obj ) | ( category ) |
is the category of operations. Every operation is a function, but not vice versa.
gap> MinimalPolynomial; <Operation "MinimalPolynomial"> gap> IsOperation(MinimalPolynomial); true gap> IsFunction(MinimalPolynomial); true gap> Factorial; function( n ) ... end gap> IsOperation(Factorial); false
‣ FunctionsFamily | ( family ) |
is the family of all functions.
The way functions are named in GAP might help to memorize or even guess names of library functions.
If a variable name consists of several words then the first letter of each word is capitalized.
If the first part of the name of a function is a verb then the function may modify its argument(s) but does not return anything, for example Append
(21.4-5) appends the list given as second argument to the list given as first argument. Otherwise the function returns an object without changing the arguments, for example Concatenation
(21.20-1) returns the concatenation of the lists given as arguments.
If the name of a function contains the word "Of
" then the return value is thought of as information deduced from the arguments. Usually such functions are attributes (see 13.5). Examples are GeneratorsOfGroup
(39.2-4), which returns a list of generators for the group entered as argument, or DiagonalOfMat
(24.12-1).
For the setter and tester functions of an attribute Attr
the names SetAttr
resp. HasAttr
are available (see 13.5).
If the name of a function contains the word "By
" then the return value is thought of as built in a certain way from the parts given as arguments. For example, creating a group as a factor group of a given group by a normal subgroup can be done by taking the image of NaturalHomomorphismByNormalSubgroup
(39.18-1). Other examples of "By
" functions are GroupHomomorphismByImages
(40.1-1) and LaurentPolynomialByCoefficients
(66.13-1).
Often such functions construct an algebraic structure given by its generators (for example, RingByGenerators
(56.1-4)). In some cases, "By
" may be replaced by "With
" (like e.g. GroupWithGenerators
(39.2-3)) or even both versions of the name may be used. The difference between StructByGenerators
and StructWithGenerators
is that the latter guarantees that the GeneratorsOfStruct
value of the result is equal to the given set of generators (see 31.3).
If the name of a function has the form "AsSomething
" then the return value is an object (usually a collection which has the same family of elements), which may, for example:
know more about its own structure (and so support more operations) than its input (e.g. if the elements of the collection form a group, then this group can be constructed using AsGroup
(39.2-5));
discard its additional structure (e.g. AsList
(30.3-8) applied to a group will return a list of its elements);
contain all elements of the original object without duplicates (like e.g. AsSet
(30.3-10) does if its argument is a list of elements from the same family);
remain unchanged (like e.g. AsSemigroup
(51.1-6) does if its argument is a group).
If Something
and the argument of AsSomething
are domains, some further rules apply as explained in Tutorial: Changing the Structure.
If the name of a function fun1
ends with "NC
" then there is another function fun2
with the same name except that the NC
is missing. NC
stands for "no check". When fun2
is called then it checks whether its arguments are valid, and if so then it calls fun1
. The functions SubgroupNC
(39.3-1) and Subgroup
(39.3-1) are a typical example.
The idea is that the possibly time consuming check of the arguments can be omitted if one is sure that they are unnecessary. For example, if an algorithm produces generators of the derived subgroup of a group then it is guaranteed that they lie in the original group; Subgroup
(39.3-1) would check this, and SubgroupNC
(39.3-1) omits the check.
Needless to say, all these rules are not followed slavishly, for example there is one operation Zero
(31.10-3) instead of two operations ZeroOfElement
and ZeroOfAdditiveGroup
.
generated by GAPDoc2HTML