Rules and Pattern Matching

A common mathematical formula is

log(x)+log(y)==log(x*y)for any x and y. The presence of the word "any" indicates that x and y can stand for arbitrary mathematical expressions in the above formula. You can use such mathematical formulas in Axiom to specify "rewrite rules". Rewrite rules are objects in Axiom that can be assigned to variables for later use, often for the purpose of simplification. Rewrite rules look like ordinary function definitions except that they are preceded by the reserved word rule. For example, a rewrite rule for the above formula is:

rule log(x) + log(y) == log(x * y)Like function definitions, no action is taken when a rewrite rule is issued. Think of rewrite rules as functions that take one argument. When a rewrite rule A=B is applied to an argument f, its meaning is "rewrite every subexpressions of f that matches A by B". The left-and side of a rewrite rule is called a pattern; its right-hand side is called its substitution. Create a rewrite rule named logrule. The generated symbol begins with a "%" and is a place holder for any other terms that might occur in the sum.

rule log(%x) + log(%y) == log(x*y)This longer notation gives you patterns that the standard notation won't handle. For example, the rule

rule %f(c * 'x) == c*%f(x)means "for all f and c, replace f(y) by c*f(x) when y is the product of c and the explicit variable x". Thus the pattern can have several adornments on the names that appear there. Normally, all of these adornments are dropped in the substitution on the right hand side. To summarize:

To enter a single rule in Axiom, use the following syntax:

rule lefthandside == righthandsideThe lefthandside is a pattern to be matched and the righthandside is its substitution. The rule is an object of type RewriteRule that can be assigned to a variable and applied to expressions to transform them.

Rewrite rules can be collected into rulesets so that a set of rules can be applied at once. Here is another simplification rule for logarithms.

rule y*log(x) == log(x**y)for any x and y. If instead of giving a single rule following the reserved word rule you give a "pile" of rules, you create what is called a ruleset. Like rules, rulesets are objects in Axiom and can be assigned to variables. You will find it useful to group commonly used rules into input files, and read them in as needed. Create a ruleset named logrules.

y*log(x) == log(x^y)only when y is an integer. The way to restrict a pattern variable y by a predicate f(y) is by using a vertical bar "|", which means "such that", in much the same way it is used in function definitions. You do this only once but at the earliest (meaning deepest and leftmost) part of the pattern. This restricts the logarithmic rule to create integer exponents only.

integral((y+exp x)/x,x) == integral(y/x,x)+Ei xfor any x and y. This rule is valid if y=0. One solution is to create a Ruleset with two rules, one with and one without y. A better solution is to use an "optional" pattern variable. Define rule eirule with a pattern variable ?y to indicate that an expression may or may not occur.