Introduction to Rules and Patterns
Rules and Patterns
CURRENT_LET_RULE_PACKAGE
is used. If a call such as LETSIMP(expr,rule_pkg_name); is made, the rule package rule_pkg_name is used for that LETSIMP command only, i.e. the value of CURRENT_LET_RULE_PACKAGE is not changed.
(C1) NONZEROANDFREEOF(X,E):= IF E#0 AND FREEOF(X,E) THEN TRUE ELSE FALSE$ (IS(E#0 AND FREEOF(X,E)) is an equivalent function definition) (C2) MATCHDECLARE(A,NONZEROANDFREEOF(X),B,FREEOF(X))$ (C3) DEFMATCH(LINEAR,A*X+B,X)$ This has caused the function LINEAR(exp,var1) to be defined. It
tests exp to see if it is of the form A*var1+B where A and B do not contain var1 and A is not zero. DEFMATCHed functions return (if the match is successful) a list of equations whose left sides are the pattern variables and parms and whose right sides are the expressions which the pattern variables and parameters matched. The pattern variables, but not the parameters, are set to the matched expressions. If the match fails, the function returns FALSE. Thus LINEAR(3*Z+(Y+1)*Z+Y**2,Z) would return [B=Y**2, A=Y+4, X=Z]. Any variables not declared as pattern variables in MATCHDECLARE or as parameters in DEFMATCH which occur in pattern will match only themselves so that if the third argument to the DEFMATCH in (C4) had been omitted, then LINEAR would only match expressions linear in X, not in any other variable. A pattern which contains no parameters or pattern variables returns TRUE if the match succeeds. Do EXAMPLE(DEFMATCH); for more examples.
CURRENT_LET_RULE_PACKAGE
The initial value of the rules is
DEFAULT_LET_RULE_PACKAGE
DEFAULT_LET_RULE_PACKAGE
This is the name of the rule package used when one is not explicitly set by the user.
MATCHDECLARE(Q,FREEOF(X,%E))
is executed, Q will match any expression not containing X or %E. If the match succeeds then the variable is set to the matched expression. The predicate (in this case FREEOF) is written without the last argument which should be the one against which the pattern variable is to be tested. Note that the patternvar and the arguments to the predicate are evaluated at the time the match is performed. The odd numbered argument may also be a list of pattern variables all of which are to have the associated predicate. Any even number of arguments may be given. For pattern matching, predicates refer to functions which are either FALSE or not FALSE (any non FALSE value acts like TRUE). MATCHDECLARE(var,TRUE) will permit var to match any expression.