Advanced Capabilities of Semantica
The last section introduced and demonstrated Semantica. This section will disclose several of the more advanced features and options with which we can control the behavior of semantic pattern matching. These include the option
Up to this point in all of our semantic pattern matching examples, there has been a conspicuous lack of any symbols besides pattern variables. What happens when free or non-pattern symbols are used in a definition or a rule? For instance, what happens when
Before we explain how each behavior is used and where, let us consider some motivating cases for the two different uses. Let us define a function that takes a polynomial in x of degree at most two and returns its coefficients. (This is hard to express with only standard structural pattern matching.)
So the solution to our problem of creating a pattern that would match polynomials and return their coefficients is to use a simple semantic pattern. If we wanted to match a strict quadratic in
Our use of
This solution is clearly not what we want.
In light of the above, when would we ever want symbols to be used semantically? Actually, we have unwittingly used them in practically every example using semantic patterns given so far. A few of the symbols we used semantically were:
Here we have used addition in and multiplication in
A further example of semantic matching with semantic symbols is the function
Above we define
Based on the above considerations, Semantica treats all symbols inside a semantic wrapper (that are not pattern variables) as being semantic, unless explicitly specified otherwise. To declare that symbol(s) should be treated structurally, we use
Now that we know how non-pattern symbols are treated, we can generalize the previous semantic rule for
To further illustrate the use of semantic symbols, let us implement an example from physics. Simple physical dynamics and elementary calculus show that a uniformly accelerating object obeys the following equation.
Let us therefore create a simple function that returns the time t taken to travel a given distance.
We can now determine how long it will take to travel a given distance, say .
Notice that the acceleration is present in the denominator. If the object is not accelerating, this answer is incorrect. To get the correct answer, we just set the acceleration to zero and again ask for the time it takes to travel the distance .
We can set the dependent variables
One can easily verify this result by explicitly solving the underlying system.
We can put in some fairly nasty conditions and the function
In most cases, Semantica can optimize its definitions, speeding up semantic pattern matching to the execution times of normal definitions. The notation for semantic patterns remains exactly the same when using optimization. Whenever the semantic pattern is non-degenerate and contains no structural symbols, optimization can be used. Degenerate patterns also can be optimized, but the solution returned cannot always be in as simple a form as when optimization is turned off. This is an intrinsic problem that we will discuss shortly. First though, let us look at some examples using optimized functions.
We can compare the timings of our simple
We see in the above case that optimization only speeds up the function about 20 times. This increase in speed typically varies from around 10 times up to several thousand times. Let us redefine the function
Here the gain in speed is around 10 times. The result of the optimized function, however, is not in quite as compact a form as that of the unoptimized function. Why is this? When optimizing is off, each semantic match, in essence, has to be solved each time. Performing a
We can circumvent the problem of unsimplified generic solution answers in most cases by enclosing the right hand side by an
Despite not always taking account of the specific arguments of the function, optimized functions do take into account all cases of the pattern variables as well as the semantic variables. For example, our time-for-distance function easily handles the case when the denominator is zero.
It should be mentioned that semantic patterns currently do not work correctly with
It should also be mentioned that optionals work nicely in conjunction with semantic patterns. We need this behavior since sometimes Mathematica has trouble with transcendental solutions.
In conclusion, Semantica is a powerful extension to Mathematica that enables one to use semantic patterns in many different situations. An intuitive description of how Semantica functions, together with some of the gritty details of how it is implemented, are given in the following sections.
Converted by Mathematica September 30, 1999 [Prev Page][Next Page]