The Mathematica Journal
Departments
Download This Issue
Home
Feature Articles
Graphics Gallery
Tricks of the Trade
In and Out
Columns
The Mathematica Programmer
New Products
New Publications
Classifieds
Calendar
News Bulletins
Editor's Pick
Mailbox
Letters
Write Us
About the Journal
Staff and Contributors
Submissions
Subscriptions
Advertising
Back Issues

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 Optional and Default and the attributes Orderless, Flat, and 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 f as

[Graphics:../Images/index_gr_20.gif]

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 [12], 2.3.9 Optional and Default Arguments).

[Graphics:../Images/index_gr_21.gif]

For those unfamiliar with optional patterns, like [Graphics:../Images/index_gr_22.gif] 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 [Graphics:../Images/index_gr_23.gif] will match 5 x with [Graphics:../Images/index_gr_24.gif] being bound to 5, but [Graphics:../Images/index_gr_25.gif] will also match x with [Graphics:../Images/index_gr_26.gif] being bound to its default of 1.

At this stage it is convenient to introduce a small utility package TableFormatting.m which defines two functions, ResultTable and CompositionTable. 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.

[Graphics:../Images/index_gr_27.gif]

We can clearly illustrate how the function f works on some simple polynomials in x using ResultTable.

[Graphics:../Images/index_gr_28.gif]

[Graphics:../Images/index_gr_29.gif]

It is obvious from the last two answers that the definition of f has serious deficiencies. First, the coefficient of x in c + a x + b x should be (a+b), not a. Also, the coefficient of x in c should be 0 whereas 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 [Graphics:../Images/index_gr_30.gif] is matching expressions that are structurally different from the pattern itself. For example, in the first evaluation above, [Graphics:../Images/index_gr_31.gif] matched x with the bindings [Graphics:../Images/index_gr_32.gif] and [Graphics:../Images/index_gr_33.gif], 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 [Graphics:../Images/index_gr_34.gif] and [Graphics:../Images/index_gr_35.gif] and the defaults of 0 and 1 for Plus and Times, respectively.

Similar behavior is true of Power. Using a pattern object such as [Graphics:../Images/index_gr_36.gif] allows Mathematica to semantically treat x as [Graphics:../Images/index_gr_37.gif]. 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, [Graphics:../Images/index_gr_38.gif] reduces to a, x+0 reduces to x, and 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 Orderless, Flat, and 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 [Graphics:../Images/index_gr_39.gif] 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.

[Graphics:../Images/index_gr_40.gif]
[Graphics:../Images/index_gr_41.gif]

The Notation package [4] 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]