Standard Mathematica and Semantic Matching
Now that the concept of a semantic pattern has been introduced--that is a pattern that can match things that are in the same mathematical form but not necessarily in the same structural form--a natural question arises. Does Mathematica already have any semantic matching capabilities? Mathematica currently has some constructs that can be used in patterns which, in a limited way, may be viewed as matching semantically instead of merely syntactically. This limited semantic-ness arises through the constructs
Default and the attributes
OneIdentity. This section will outline why these constructs can be thought of as semantic and also why they are only semantic in a very limited sense.
Let us define a function
f which works on any expression linear in
x and returns a function
h of the coefficients. We could define
The above definition of
f uses standard pattern matching but is limited. Alternatively, we could have defined
f more concisely by using optional patterns (cf. The Mathematica Book , 2.3.9 Optional and Default Arguments).
For those unfamiliar with optional patterns, like above, think of them as matching a part of an expression if the part is present but matching a default if that part is not present. For instance, the pattern will match
5 x with being bound to
5, but will also match
x with being bound to its default of
At this stage it is convenient to introduce a small utility package
TableFormatting.m which defines two functions,
CompositionTable takes a list of functions and a list of arguments and returns a formatted table of each function applied to each argument.
ResultTable is a simple function which takes expressions and returns a nicely formatted table of the expressions and their evaluations.
We can clearly illustrate how the function
f works on some simple polynomials in
It is obvious from the last two answers that the definition of
f has serious deficiencies. First, the coefficient of
c + a x + b x should be
a. Also, the coefficient of
c should be
f[c] did not evaluate. We could, of course, add more specialized rules and fix the deficiencies of the implementation of
f that uses
Optional; but the resulting function would not be immediately recognizable, and the definition of
f would get progressively more convoluted for matching more complicated functions.
Even though the last definition of
f does not fulfill all our requirements, we can observe that the pattern is matching expressions that are structurally different from the pattern itself. For example, in the first evaluation above, matched
x with the bindings and , but clearly the pattern and the expression have structurally different forms. This implies that standard Mathematica has a degree of semantic-ness to its pattern matching. In essence, we can make Mathematica recognize that something is semantically the same as something
+ 0, and also that something is semantically the same as
1 * something. This occurs through the use of the optional patterns and and the defaults of
Similar behavior is true of
Power. Using a pattern object such as allows Mathematica to semantically treat
x as . Again, we have a pattern which will match structurally different but semantically equivalent expressions. Recognizing such semantic equivalence without using the pattern construct
Optional is hard (or tedious as we saw above) since in almost all cases, expressions are reduced to a canonical form; for example,
x+0 reduces to
1 y reduces to
y. Therefore, when we are structurally looking for these cases, they are no longer present.
In a similar manner, the attributes
OneIdentity allow Mathematica to match expressions not in the same structural form as a given pattern. In summary, in the very limited ways described above, one can indeed view Mathematica as having some inherent semantic-ness in its pattern matching.
In the following sections
E is used in several places inside examples. For consistency with Mathematica version 4, and because it looks more readable, let us use the notation as the symbol for exponentiation, in both input and output. After we have loaded the Notation package (which is loaded by Semantica in any case) we can accomplish this by the following trivial notation statement.
The Notation package  is included in the standard installation of Mathematica. Throughout this article we will add and use notations as necessary to make the article readable, although the focus is not on the notations themselves. The interested reader should consult the Notation documentation included in the extra packages directory of Mathematica.
Converted by Mathematica
September 30, 1999
[Prev Page][Next Page]