REDUCE offers various ways to use symbolic routines in algebraic mode such that they appear on the algebraic level as genuine parts of REDUCE. These mainly differ in their strategy of evaluating parameters (and results).
Some general protocol rules apply for REDUCE symbolic mode routines:
The simplest way to link a symbolic procedure to algebraic mode is the symbolic operator declaration. In that case the routine can be called by its proper name in algebraic mode with REDUCE first evaluating its parameters to fully simplified algebraic forms. Upon return the result should also be an algebraic form (or NIL). Example:
This routine receives two algebraic expressions and computes their sum by calling the algebraic evaluator. The calls the the LISP function print have been inserted here and in the following examples to show you the forms passed to the routine.
If the symbolic routine is specialized for computations with pure polynomials it can be declared polyfn. REDUCE will evaluate the arguments for such a routine to standard forms and expects that the result also will have that form. Example:
This routine also adds its arguments but it is specialized for polynomials. If called with a non-polynomial form an error message will be generated. In the put statement the first argument is the algebraic operator name and the third one is the name of the associated evaluator function. These may differ.
The most general type of function is that of a psopfn. REDUCE will not evaluate the parameters for such a routine, instead it passes the unevaluated list of parameters to the function. The routine has to evaluate its parameters itself (of course using the services of reval and friends). So a psopfn can handle variable numbers of parameters. The result must be an algebraic expression or nil. Example:
This routine can be called with an arbitrary number of parameters; it will evaluate them one after the other, add them by calling reval and return the sum as result. Note that the name used in algebraic mode is multi_plus which is different from the name of the symbolic routine doing the work. This is necessary because otherwise the argument count check of REDUCE would complain as soon as the routine is called with more than one argument.
In the next example a psopfn implements an operator with a fixed number of arguments; it expects two numbers as arguments and performs an extensive checking as any such routine should do; again the name of the routine and the algebraic mode operator are different:
The functions typerr and rederr are explained in the section error management.
When you declare a new algebraic operator and want to assign a special evaluation mode for it which cannot be written in algebraic mode (e.g. as a rule set) you can create a simplifier which has the task to convert each operator expression to a standard quotient. A simplifier function is linked to the operator by the property simpfn and has one argument which is the unevaluated parameter list of the operator expression. It will be called every time the operator appears in an expression. It must return a standard quotient. Example:
In many cases the simpfn is the method of choice as it is best integrated into the REDUCE evaluation process: its results can immediately be used for subsequent calculations while algebraic form results need to be converted to standard quotients before combining them with other formulae.
Note that a simpfn which cannot simplify its argument must nevertheless return a standard quotient, then with the unevaluable form as kernel. E.g. a function for supporting the algebraic evaluation of the operator “<”:
Here all comparisons with numeric items are reduced to zero or one because of the equivalence of zero and “false” in algebraic mode, while in all other cases the non-evaluable expression is returned mapped by the function mksq which converts an algebraic expression into a standard quotient, ensuring the identity of composite kernels (see the section standard forms) .