In the Maude implementation, rewriting modulo
is accomplished by
using a matching modulo
algorithm.
More precisely, given an
equational theory
, a term
(corresponding to the
lefthand side of an equation) and a subject term
, we say that
matches
modulo
(or that
-matches
) if there is a
substitution
such that
, that is,
and
are equal modulo the equational theory
(compare with the
syntactic definition of matching in Section 4.7 above).
Given an equational theory
corresponding to all the attributes declared
in different binary operators, Maude synthesizes a combined matching
algorithm for the theory
, and does both equational simplification
(with equations) and rewriting (with rules in system modules, see
Chapter 5) modulo the axioms
.
Note, however, that for operators
whose equational axioms
include the
associativity axiom, to achieve the effect of simplification modulo
using
an
-matching algorithm, we have to attempt matching a lefthand side of the
form
not only on a subject term
, but also on all its
-subterms, that is, on those ``fragments'' of the top structure of the term
that could be matched by
. For example, assuming a binary
associative operator f and constants a, b, c, and
d of the appropriate sort, the term
does not match
the term
, that is, there is no substitution making both
terms equal modulo associativity; however, because of associativity of f,
is equivalent to f(f(a, b), f(c, d)) and then
trivially matches the first
subterm. This becomes easier to see using mixfix notation; if ![]()
_._,
then
a . b and
a . b . c . d, and we clearly see that
matches a fragment of
. For the case where the only axiom is
associativity, the _._-subterms of a . b . c . d are
a . b a . b . c b . c b . c . d c . d
If the operation _._ had been declared both associative and
commutative, then we should add to those the additional subterms
a . c a . d b . d a . b . d a . c . d
If the term
matches either
or an
-subterm of
modulo
, then we say that
matches
with
extension modulo
(or that
-matches
with
extension).
For example, the lefthand side of the equation a . b = e
matches a . b . c . d with extension modulo associativity, and the
lefthand side of a . d = g matches a . b . c . d with extension
modulo associativity and commutativity.
For
a binary operator with equational attributes
including the
associativity axiom, we now define how a subject term
is
-rewritten with extension using an equation
. First of all,
must
-match with extension a
maximal
-subterm
of
(that is, an
-subterm of
that is
not itself an
-subterm of a bigger
-subterm). This means that there is an
-subterm
of
and a substitution
such that
. Then, the corresponding
-rewriting with extension step
rewrites
to the term obtained by replacing the subterm
by
.
Note that a term
-matches with extension a maximal
-subterm if and only if it
-matches without extension some
-subterm. This is of course the important practical advantage of
-matching and
-rewriting with extension, namely, that only maximal
-subterms of a term have to be inspected to get the effect of rewriting
equivalence classes modulo
. For more technical details on rewriting
modulo a set of axioms, see, e.g., [23].
Matching with extension for an associative operator essentially corresponds
to matching without extension for a collection of associated equations.
For example, we could have ``generalized'' the equation a . b = e
with _._ associative to the equations
eq a . b = e . eq X . a . b = X . e . eq a . b . Y = e . Y . eq X . a . b . Y = X . e . Y .
so that we could have achieved the same effect by rewriting only
at the top of maximal
-subterms (without extension). Similarly, for
_._ associative and commutative, we could have generalized the
same equation a . b = e to the equations
eq a . b = e . eq a . b . Y = e . Y .
In Maude this generalization does not have to be performed explicitly as a
transformation of the specification. It is instead achieved implicitly in a
built-in way by performing
-matching with extension. If the equational
axioms declared for a binary operator
include the associativity axiom, then
a subject term
with
as its top operator is internally represented (but
this representation can also be externally used, see
Section 3.9.3) as the flattened term
,
with the
having top operators
different from
. Furthermore, if a (two-sided) identity element
has been
declared for
, then
. That is, we assume in
this case that all identities have been simplified away.
Relative to this internal representation, it is then easy to define the notion
of an
-subterm. If the axioms of
include associativity but not
commutativity, then the
-subterms of the term
are all terms of the form
with
and
.
Similarly, if the axioms of
include associativity and commutativity, then
the
-subterms of
are all terms of the form
with
, and
.
The concepts of positions in a term and depth of a term, that are important in many situations, refer to this flattened form. The compact notation for terms constructed with operators having the iter attribute (Section 4.4.2) is also considered a form of flattened notation, so that, for the purpose of calculating term depth, if the top level is at level 0, then the occurrence of X:Foo in f^3(X:Foo) is at level 1, not level 3.
Adding axioms for an identity element
to a possibly associative and/or
commutative operation
leads to some subtle cases, where
the proper application of the general notions may not always
coincide with the user's expectations. To begin with, unexpected cases of
nontermination may be introduced by an unwary user.
For example, the equation
eq a . X = b . a .
will cause nontermination when _._ is declared associative
with identity 1, since we have, for example,

A second source of unexpected behavior is the fact that a lefthand side involving an associative operator may, in the presence of an additional identity attribute, match a term not involving at all that operator. Thus, for the above equation, we have also the nonterminating chain of rewriting steps

In a similar way, in the presence of an identity element, the user's
expectations about when a lefthand side will match with extension a subject
term may not fully agree with the proper technical definition. Consider, for
example, a binary operation _._ that is associative and commutative,
and that has an identity element 1, and let
eq a . X = c .
be an equation. Then,
a . X matches the subject term a modulo
the axioms by instantiating X to 1, giving rise to the
simplification
a . b . c
with extension in three different ways, namely, with substitutions
a . b . c, the substitution
a . 1 is not a
_._-subterm of the term a . b . c. However, because of item
1 above, we know that the equation will match that way not at the top,
but ``one level down,'' leading to the simplification
It is also important to realize that there is no match with extension
between the lefthand side of the equation a = b and the subject term
a . b . c (because the lefthand side a is not a _._-term),
although again the equation will match that way not
at the top, but ``one level down,'' leading to the simplification
Of course, lefthand sides may contain several operators, each matched modulo a different theory. Maude will then match each fragment of a lefthand side according to its given theory.
Consider, for example, the following specification where _._
is associative and _+_ is associative and commutative:
fmod XMATCH-TEST is
sort Elt .
ops a b c d e : -> Elt .
op _._ : Elt Elt -> Elt [assoc] .
op _+_ : Elt Elt -> Elt [assoc comm] .
vars X Y Z : Elt .
eq X . (Y + Z) = (X . Y) + (X . Z) [metadata "distributivity"] .
endfm
The lefthand side of the distributivity equation will produce 12 matches with extension for the subject term
a . b . (c + d + e)
Enumerating these by hand would be tedious and error prone, however Maude provides the xmatch command (see also Section 16.3) for just this purpose:
xmatch X . (Y + Z) <=? a . b . (c + d + e) .
The output given by Maude consists of the substitution for each match with extension together with the portion of the subject actually matched:
Maude> xmatch X . (Y + Z) <=? a . b . (c + d + e) . xmatch in XMATCH-TEST : X . Z + Y <=? a . b . c + d + e . Decision time: 0ms cpu (0ms real) Solution 1 Matched portion = (whole) X:Elt --> a . b Y:Elt --> c Z:Elt --> d + e Solution 2 Matched portion = b . (c + d + e) X:Elt --> b Y:Elt --> c Z:Elt --> d + e Solution 3 Matched portion = (whole) X:Elt --> a . b Y:Elt --> d Z:Elt --> c + e Solution 4 Matched portion = b . (c + d + e) X:Elt --> b Y:Elt --> d Z:Elt --> c + e Solution 5 Matched portion = (whole) X:Elt --> a . b Y:Elt --> e Z:Elt --> c + d Solution 6 Matched portion = b . (c + d + e) X:Elt --> b Y:Elt --> e Z:Elt --> c + d Solution 7 Matched portion = (whole) X:Elt --> a . b Y:Elt --> c + d Z:Elt --> e Solution 8 Matched portion = b . (c + d + e) X:Elt --> b Y:Elt --> c + d Z:Elt --> e Solution 9 Matched portion = (whole) X:Elt --> a . b Y:Elt --> c + e Z:Elt --> d Solution 10 Matched portion = b . (c + d + e) X:Elt --> b Y:Elt --> c + e Z:Elt --> d Solution 11 Matched portion = (whole) X:Elt --> a . b Y:Elt --> d + e Z:Elt --> c Solution 12 Matched portion = b . (c + d + e) X:Elt --> b Y:Elt --> d + e Z:Elt --> c
Note that extension is only used for matching the top operation, _._ in
this example, but not _+_. This is the reason why the subterm Y + Z of
the lefthand side should match the entire maximal _+_-subterm
of the subject term, and not just some _+_-subterm.
For operators with the iter attribute, the situation with matching is analogous to the assoc theory, so that proper subterms of say f^3(X:Foo), such as f^2(X:Foo) and f(X:Foo), can also be matched by means of extension.