Usually no operations are defined for record. However a record may
contain a special operations
record that contains functions that are
called when this record is an operand of a binary operation. This
mechanism is detailed below for the addition.
obj + rec
, rec + obj
If either operand is a record, and if this record contains an element
with name operations
that is a record, and if this record in turn
contains an element with the name +
that is a function, then this
function is called with the two operands as arguments, and the value of
the addition is the value returned by that function. If both operands
are records with such a function rec.operations.+
, then the function
of the right operand is called. If either operand is a record, but
neither operand has such a function rec.operations.+
, an error is
signalled.
obj - rec
, rec - obj
obj * rec
, rec * obj
obj / rec
, rec / obj
obj mod rec
, rec mod obj
obj ^ rec
, rec ^ obj
This is evaluated similar, but the functions must obviously be called
-
, *
, /
, mod
, ^
respectively.
The following example shows one piece of the definition of a residue
classes, using record operations. Of course this is far from a complete
implementation (see About Defining New Group Elements). Note that the
*
must be quoted, so that it is taken as an identifier (see
Identifiers).
gap> ResidueClassOps := rec( );; gap> ResidueClassOps.\* := function ( l, r ) > if l.modulus <> r.modulus then > Error("<l> and <r> must have the same modulus"); > fi; > return rec( > representative := (l.representative * r.representative) > mod l.modulus, > modulus := l.modulus, > operations := ResidueClassOps ); > end;; gap> ResidueClass := function ( representative, modulus ) > return rec( > representative := representative, > modulus := modulus, > operations := ResidueClassOps ); > end;; gap> l := ResidueClass( 13, 23 );; gap> r := ResidueClass( -1, 23 );; gap> s := l * r; rec( representative := 10, modulus := 23, operations := rec( \* := function ( l, r ) ... end ) )
GAP 3.4.4