]>
A common mathematical formula is The presence of ``'' indicates that and 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:
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 is applied to an argument , its meaning is: ``rewrite every subexpression of that matches by '' The left-hand side of a rewrite rule is called a pattern; its right-side side is called its substitution.
Create a rewrite rule named logrule. The generated symbol beginning with a ``%'' is a place-holder for any other terms that might occur in the sum.
Create an expression with logarithms.
Apply logrule to .
The meaning of our example rewrite rule is: ``for all expressions and , rewrite by .'' Patterns generally have both operation names (here, log and ``+'') and variables (here, and ). By default, every operation name stands for itself. Thus log matches only ``'' and not any other operation such as sin. On the other hand, variables do not stand for themselves. Rather, a variable denotes a pattern variable that is free to match any expression whatsoever. pattern:variables
When a rewrite rule is applied, a process called pattern matching goes to work by systematically scanning pattern:matching the subexpressions of the argument. When a subexpression is found that ``matches'' the pattern, the subexpression is replaced by the right-hand side of the rule. The details of what happens will be covered later.
The customary Axiom notation for patterns is actually a shorthand for a longer, more general notation. Pattern variables can be made explicit by using a percent ``%'' as the first character of the variable name. To say that a name stands for itself, you can prefix that name with a quote operator ``'''. Although the current Axiom parser does not let you quote an operation name, this more general notation gives you an alternate way of giving the same rewrite rule:
This longer notation gives you patterns that the standard notation won't handle. For example, the rule
means ``for all and , replace by when is the product of and the explicit variable .''
Thus the pattern can have several adornments on the names that appear there. Normally, all 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
rule leftHandSide == rightHandSide
The 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. If instead of giving a single rule following the reserved word you give a ``pile'' of rules, you create what is called a ruleset. 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 .
Again, create an expression containing logarithms.
Apply the ruleset logrules to .
We have allowed pattern variables to match arbitrary expressions in the above examples. Often you want a variable only to match expressions satisfying some predicate. For example, we may want to apply the transformation only when is an integer.
The way to restrict a pattern variable by a predicate pattern:variable:predicate is by using a vertical bar ``|'', which means ``such that,'' in such that much the same way it is used in function definitions. predicate:on a pattern variable 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.
Compare this with the result of applying the previous set of rules.
You should be aware that you might need to apply a function like integer within your predicate expression to actually apply the test function.
Here we use integer because has type Expression Integer but even? is an operation defined on integers.
Here is the application of the rule.
This is an example of some of the usual identities involving products of sines and cosines.
Another qualification you will often want to use is to allow a pattern to match an identity element. Using the pattern , for example, neither nor matches the expression . Similarly, if a pattern contains a product or an exponentiation , then neither or matches .
If identical elements were matched, pattern matching would generally loop. Here is an expansion rule for exponentials.
This rule would cause infinite rewriting on this if either or were allowed to match .
There are occasions when you do want a pattern variable in a sum or product to match or . If so, prefix its name with a ``?'' whenever it appears in a left-hand side of a rule. For example, consider the following rule for the exponential integral: This rule is valid for . One solution is to create a Ruleset with two rules, one with and one without . A better solution is to use an ``optional'' pattern variable.
Define rule eirule with a pattern variable to indicate that an expression may or may not occur.
Apply rule eirule to an integral without this term.
Apply rule eirule to an integral with this term.
Here is one final adornment you will find useful. When matching a pattern of the form to an expression containing a long sum of the form , there is no way to predict in advance which subset of the sum matches and which matches . Aside from efficiency, this is generally unimportant since the rule holds for any possible combination of matches for and . In some situations, however, you may want to say which pattern variable is a sum (or product) of several terms, and which should match only a single term. To do this, put a prefix colon ``:'' before the pattern variable that you want to match multiple terms. pattern:variable:matching several terms
The remaining rules involve operators and . operator
These definitions tell Axiom that and are formal operators to be used in expressions.
First define myRule with no restrictions on the pattern variables and .
Apply myRule to an expression.
Define myOtherRule to match several terms so that the rule gets applied recursively.
Apply myOtherRule to the same expression.
Summary of pattern variable adornments:
(x | predicate?(x)) | means that the substutution for must satisfy predicate(s) = true. |
?x | means that can match an identity element (0 or 1). |
:x | means that can match several terms in a sum. |
Here are some final remarks on pattern matching. Pattern matching provides a very useful paradigm for solving certain classes of problems, namely, those that involve transformations of one form to another and back. However, it is important to recognize its limitations. pattern:matching:caveats
First, pattern matching slows down as the number of rules you have to apply increases. Thus it is good practice to organize the sets of rules you use optimally so that irrelevant rules are never included.
Second, careless use of pattern matching can lead to wrong answers. You should avoid using pattern matching to handle hidden algebraic relationships that can go undetected by other programs. As a simple example, a symbol such as ``J'' can easily be used to represent the square root of or some other important algebraic quantity. Many algorithms branch on whether an expression is zero or not, then divide by that expression if it is not. If you fail to simplify an expression involving powers of to algorithms may incorrectly assume an expression is non-zero, take a wrong branch, and produce a meaningless result.
Pattern matching should also not be used as a substitute for a domain. In Axiom, objects of one domain are transformed to objects of other domains using well-defined coerce operations. Pattern matching should be used on objects that are all the same type. Thus if your application can be handled by type Expression in Axiom and you think you need pattern matching, consider this choice carefully. Expression You may well be better served by extending an existing domain or by building a new domain of objects for your application.