Rule lists offer an alternative approach to defining substitutions that is different from either SUB or LET. In fact, they provide the best features of both, since they have all the capabilities of LET, but the rules can also be applied locally as is possible with SUB. In time, they will be used more and more in REDUCE. However, since they are relatively new, much of the REDUCE code you see uses the older constructs.
A rule list is a list of rules that have the syntax
The tilde preceding a variable marks that variable as free for that rule, much as a variable in a FOR ALL clause in a LET statement. The first occurrence of that variable in each relevant rule must be so marked on input, otherwise inconsistent results can occur. For example, the rule list
designed to replace products of cosines, would not be correct, since the second rule would only apply to the explicit argument X. Later occurrences in the same rule may also be marked, but this is optional (internally, all such rules are stored with each relevant variable explicitly marked). The optional WHEN clause allows constraints to be placed on the application of the rule, much as the SUCH THAT clause in a LET statement.
A rule list may be named, for example
Such named rule lists may be inspected as needed. E.g., the command trig1; would cause the above list to be printed.
Rule lists may be used in two ways. They can be globally instantiated by means of the command LET. For example,
would cause the above list of rules to be globally active from then on until cancelled by the command CLEARRULES, as in
CLEARRULES has the syntax
The second way to use rule lists is to invoke them locally by means of a WHERE clause. For example
The syntax of an expression with a WHERE clause is:
so the first example above could also be written
The effect of this construct is that the rule list(s) in the WHERE clause only apply to the expression on the left of WHERE. They have no effect outside the expression. In particular, they do not affect previously defined WHERE clauses or LET statements. For example, the sequence
would result in the output
Although WHERE has a precedence less than any other infix operator, it still binds higher than keywords such as ELSE, THEN, DO, REPEAT and so on. Thus the expression
will parse as
WHERE may be used to introduce auxiliary variables in symbolic mode expressions, as described in Section 17.4. However, the symbolic mode use has different semantics, so expressions do not carry from one mode to the other.
Compatibility Note: In order to provide compatibility with older versions of rule lists released through the Network Library, it is currently possible to use an equal sign interchangeably with the replacement sign => in rules and LET statements. However, since this will change in future versions, the replacement sign is preferable in rules and the equal sign in non-rule-based LET statements.
Some advanced features of the rule list mechanism make it possible to write more complicated rules than those discussed so far, and in many cases to write more compact rule lists. These features are:
A free operator in the left hand side of a pattern will match any operator with the same number of arguments. The free operator is written in the same style as a variable. For example, the implementation of the product rule of differentiation can be written as:
The double slash operator may be used as an alternative to a single slash (quotient) in order to match quotients properly. E.g., in the example of the Gamma function above, one can use:
The above example suffers from the fact that two rules had to be written in order to perform the required operation. This can be simplified by the use of double tilde variables. E.g. the rule list
will implement the same operation in a much more compact way. In general, double tilde variables are bound to the neutral element with respect to the operation in which they are used.
|Pattern given||Argument used||Binding|
|~z + ~~y||x||z=x; y=0|
|~z + ~~y||x+3||z=x; y=3 or z=3; y=x|
|~z * ~~y||x||z=x; y=1|
|~z * ~~y||x*3||z=x; y=3 or z=3; y=x|
|~z / ~~y||x||z=x; y=1|
|~z / ~~y||x/3||z=x; y=3|
Remarks: A double tilde variable as the numerator of a pattern is not allowed. Also, using double tilde variables may lead to recursion errors when the zero case is not handled properly.
The operator SHOWRULES takes a single identifier as argument, and returns in rule-list form the operator rules associated with that argument. For example:
Such rules can then be manipulated further as with any list. For example rhs first ws; has the value 1. Note that an operator may have other properties that cannot be displayed in such a form, such as the fact it is an odd function, or has a definition defined as a procedure.
If rules have overlapping domains, their order of application is important. In general, it is very difficult to specify this order precisely, so that it is best to assume that the order is arbitrary. However, if only one operator is involved, the order of application of the rules for this operator can be determined from the following:
Example: The following rule set enables the computation of exact values of the Gamma function:
Here, rule by rule, cases of known or definitely uncomputable values are sorted out; e.g. the rule leading to the error expression will be applied for negative integers only, since the positive integers are caught by the preceding rule, and the last rule will apply for negative odd multiples of 1∕2 only. Alternatively the first rule could have been written as
but then the case x = 1∕2 should be excluded in the WHEN part of the last rule explicitly because a rule without free variables cannot take precedence over the other rules.