Narrowing is a generalization of term rewriting that allows free variables in terms (as in logic programming) and replaces pattern matching by unification in order to (non-deterministically) reduce these terms. Narrowing was originally introduced as a mechanism for solving equational unification problems [54] and then generalized to solve the more general problem of symbolic reachability [86]. The narrowing mechanism has a number of important applications including automated proofs of termination [6], execution of functional-logic programming languages [58, 30, 61, 95, 79], partial evaluation [4], verification of cryptographic protocols [86], and equational unification [65], to mention just a few.
At each rewriting step one must choose which subterm of the subject term and which rule of the
specification are going to be considered. Similarly, at each narrowing step one must choose which
subterm of the subject term, which rule of the specification,and which instantiation on the variables of
the subject term and the rule’s lefthand side are going to be considered. The narrowing relation is
formally defined as follows. Let
= (Σ,Ax,R) be an order-sorted rewrite theory where R is a set of
unconditional rewrite rules specified with the rl keyword and Ax is a set of commonly occurring
axioms (declared in Maude as equational attributes, see Section 4.4.1), such that an Ax-unification
procedure is available in Maude (only unification for order-sorted signatures with free, C, or
AC function symbols is available at this moment, see Chapter 12). Let CSUAx(u = u′)
provide1 a
finitary and complete set of unifiers for any pair of terms u,u′ with the same top sort. The
R,Ax-narrowing relation on
Σ(
) is defined as t
σ,p,R,Ax t′ (or
σ when p,R,Ax are
understood) if there is a non-variable position p of t, a (possibly renamed) rule l → r in R,
and a unifier σ
CSUAx(t|p = l) such that t′ = (t[r]p)σ. We denote by t ↝σ,R,Ax+t′
(resp. t ↝σ,R,Ax*t′) the transitive (resp. reflexive-transitive) closure of the narrowing relation,
where σ is obtained as the composition of the substitutions for each narrowing step in the
sequence.
The difference between a rewriting step and a narrowing step is that in both cases we use a rewrite rule l → r to rewrite t at a position p in t, but narrowing unifies the lefthand side l and the chosen subject term t|p before actually performing the rewriting step. Also, narrowing is usually2 restricted to non-variable positions of t, whereas rewriting does not require such a restriction.
Consider the following system module defining the addition function _+_ on natural numbers built from 0 and s:
Consider the term X + s(0) and the two rules base and ind. Narrowing will instantiate variable X with 0 and s(X’) respectively in order to be able to apply each of these rules, i.e., the following two narrowing steps are generated:

0},base s(0)
↝{X s(#1:Nat)},ind s(#1 : Nat + s(0)) |
↝{#1:Nat 0},base s(s(0)) |
↝{X s(#1:Nat)},ind s(#1 : Nat + s(0)) |
↝{#1:Nat s(#2:Nat)},ind s(s(#2 : Nat + s(0))) |
↝{#2:Nat 0},base s(s(s(0))) |
and so on.
The following infinite narrowing derivation resulting from applying rule ind infinitely many times can also be shown:

Given an order-sorted rewrite theory (Σ,Ax,R), a reachability problem is of the form ∃X1,…,Xk s.t. s →*t, where X1,…,Xk are all the variables of s and t. Such a reachability problem can be solved by narrowing modulo Ax, yielding a (possibly infinite) set of substitutions σ1,…,σn,… that bind some variables of X1,…,Xk in such a way that σi(s) →R,Ax*σi(t) for each i.
Due to nontermination, narrowing behaves as a semi-decision procedure for equational unification
and for reachability analysis in a wide variety of theories. However, for some particular subject
terms narrowing may terminate, providing a complete set of solutions. For instance, in the
above Maude module, narrowing computes the solution {X
s(Y)} for the reachability
problem ∃X,∃Y s.t. 0 + X →*s(Y) and it terminates with no more solutions. Instead, for the
reachability problem ∃X,∃Z s.t. X + s(0) →*s(s(Z)), narrowing computes the solution
{X
s(0), Z
0} but it cannot terminate because of the above infinite narrowing sequence using
ind. Moreover, narrowing cannot prove that the reachability problem ∃X s.t. X + s(0) →*0
does not have a solution, again because of the above infinite narrowing sequence using
ind.
We have implemented a version of narrowing with simplification, which is slightly different from
the one formally defined above. Let
= (Σ,E ∪ Ax,R) be an order-sorted rewrite theory where R
and Ax are defined as above and E are the remaining equations specified with the eq or ceq
keywords. Each narrowing step of the form t
σ,p,R,Ax t′ is followed by simplification using the
relation →E,Ax!, i.e., the combined relation (
σ,p,R,Ax;→E,Ax!) is defined as t
σ,p,R,Ax;→E,Ax!t′′
iff t
σ,p,R,Ax t′, t′→E,Ax*t′′, and t′′ is E,Ax-irreducible. Note that this combined relation
(
σ,p,R,Ax;→E,Ax!) may be incomplete, i.e., given a reachability problem of the form t →*t′ and a
solution σ (i.e., σ(t) →R,E∪Ax*σ(t′)), the relation
σ,p,R,Ax;→E,Ax! may not be able to find a more
general solution. The reason is that the equations E should also be executed by narrowing
instead of rewriting to ensure completeness under appropriate conditions (see [86] and
Section 16.2). However, the combination of narrowing using rules and axioms with simplification
using equations and axioms can be quite helpful to allow built-in Maude functions such as
addition or multiplication, which cannot be executed by narrowing in their predefined
form.
Note that for any narrowing sequence t ↝σ,R,Ax*s, we have a corresponding rewrite sequence σ(t) →R,Ax*s. However, only under appropriate conditions is narrowing complete as an equational unification algorithm [65, 66], or as a procedure to solve reachability problems [86]. That is, given a reachability problem3 ∃X1,…,Xk s.t. s →*t completeness means that for each possible substitution ρ that binds some variables of X1,…,Xk in such a way that ρ(s) →R,Ax*ρ(t), and for all the substitutions σ1,…,σn,… provided by narrowing from s, there is an index i and two substitutions θ,τ such that ρ(s) →R,Ax*θ(s), ρ(t) →R,Ax*θ(t), θ(s) →R,Ax*θ(t), and θ = Axσi;τ, where σi;τ denotes substitution composition in diagrammatic order, i.e., (σi;τ)(X) = τ(σi(X)). Essentially, completeness holds either
The narrowing relation is defined on top of the order-sorted unification procedure described in Chapter 12. As described there, Maude 2.4 currently provides an order-sorted Ax-unification algorithm for all order-sorted theories (Σ,E ∪ Ax) such that:
Explicitly excluded are theories with binary function symbols having either: (i) the id:, left id:, or right id: attributes; or (ii) the assoc attribute without the comm one; or (iii) a combination of (i) and (ii) (for example, a binary symbol with only assoc and id: attributes).
Let mod (Σ,E ∪ Ax,R) endm be an order-sorted system module where R is a set of rewrite rules specified with the rl or crl keywords, Ax is a set of commonly occurring axioms (declared in Maude as equational attributes, see Section 4.4.1) such that (Σ,Ax) satisfies the restrictions mentioned above and in Chapter 12, and E are the remaining equations specified with the eq or ceq keywords. Furthermore, the rules R must satisfy the following extra conditions:
For example, for Ax = AC, coherence modulo AC is easily achieved by adding to each rule in R with a top AC function symbol in its lefthand side a similar rule with an extra “extension variable” argument added to the AC function symbol, as explained in Section 4.8.
We recall again that the equations E in the system module are disregarded for narrowing purposes. However, they are applied for simplification after each narrowing step, as it is performed in Maude for rewriting. Recall, again, that this combination of one narrowing step followed by equational simplification is not complete. A full treatment of rules, equations, and axioms for narrowing is outside the scope of the present implementation and is left for future work.
Furthermore, frozen arguments (see Section 4.4.9) are allowed for narrowing, as for rewriting, and are given the standard meaning of not allowing any narrowing step below such frozen arguments, just as in the context-sensitive narrowing of [70].
Finally, we do not consider any narrowing strategy at all for solving reachability problems, i.e., all positions in a term with an admissible R,Ax-narrowing step are explored.
Given a system module ⟨ModId⟩, the user can give to Full Maude a search command of the form:
] in ⟨ModId⟩ : ⟨Term-1⟩ ⟨SearchArrow⟩ ⟨Term-2⟩ .)
where:
The one step arrow ~>1 is an abbreviation for the one-or-more steps arrow ~>+ with the depth bound m set to 1.
Consider, for example, the following variant of the vending machine for buying apples (a) or cakes (c) with dollars ($) and quarters (q) used in different parts of the manual, where now the rules are explicitly coherent and the lefthand sides are non-variable terms (this theory does indeed satisfy the completeness requirements for reachability mentioned in [86] and Section 16.2 above):
We can use the narrowing search command to answer the question:
Is there any combination of one or more coins that returns exactly an apple and a cake?
This is done by searching for states that have a variable of sort Money instead of sort Marking at the starting term and match a corresponding pattern at the end.
Note that we must restrict the depth, because narrowing does not terminate for this reachability problem even though the above two solutions are indeed the only solutions. The problem is that narrowing follows a breadth-first exploration and does not stop until the whole narrowing tree demanded by the search command is created, even though this infinite search may not yield any further solutions. The very same problem happens for the standard search command (see Section 5.4.3). Indeed, if we increase the depth of the narrowing tree, we can experimentally observe that no more solutions are found.
As with the search command of Full Maude (see Section 15.5), the narrowing version does not provide paths to solutions.
We can formulate a more complex question for our vending machine:
Is there any combination of one or more coins that returns an apple and a cake and is such that some extra money is left but that extra money cannot be used to buy anything else?
The fact that some money is left is characterized by including a variable of sort Money in the final state, and the fact that nothing can be bought is characterized by using the ~>! arrow instead of ~>*.
There are a couple of issues related to this reachability problem:
The reason is that any state term enclosed by the <_> operator and containing a variable of sort Marking or Money does have a narrowing step from it and, thus, the state < q a c #1:Money > would not be strongly irreducible for narrowing.
Now the reason is that there is no rule that introduces symbols q by unification, so the substitution M:Money --> $ $ q q cannot be computed. This does not mean that narrowing is incomplete (it is indeed complete for this theory). This solution is indeed a substitution instance of the solution mentioned in (1) above.
The reason why we do not obtain these solutions is our restriction to strongly irreducible final states. But if we give the very same command but using the ~>* arrow instead of ~>!, we do obtain the substitution
described above. The somewhat subtle point is that the narrowing sequence

Another point of interest is the appearance of variables of the form #n:Sort, which are called fresh and are described in Chapter 12. Unification modulo axioms usually introduces fresh variables; furthermore, narrowing introduces many fresh variables because the rule applied at each narrowing step is appropriately renamed so that no variable is shared by it and the current term. Indeed, the standard solution used in logic and functional-logic programming language implementations is to use a counter along each narrowing derivation to ensure that fresh variables have never been used previously in that narrowing derivation. This method is called standardized apart.
Although rewriting does not allow extra variables in the righthand side of rules6 , extra variables in righthand sides pose no problem for narrowing. Since rules having extra variables in the righthand side are not allowed in Maude for rewriting purposes, the attribute nonexec (see Section 4.5.3) must be added to such rules if one wants to use them for narrowing. The nonexec attribute is not taken into account by narrowing: all unconditional rules, regardless of whether or not they include the nonexec attribute, are used by narrowing. Extra variables in the righthand side are a common feature of programs using narrowing as the operational evaluation mechanism, as in logic programming or functional-logic programming (see [61]). Let us motivate this feature with an example. Consider the following program defining the concatenation of two lists and the function last:
In the rule
we have used an extra variable N to denote the last element of the list and used a constraint
that narrowing will solve by instantiating N in the proper way. Furthermore, note the use of the kind [Nat] and the value error in order to describe what a successful solution is. The following reachability problem is solved by narrowing but cannot be solved by rewriting due to the extra variable in the last rule.7
As described in Chapter 12 and also recalled in Section 16.3, Maude 2.4 currently provides an order-sorted Ax-unification algorithm for all order-sorted theories (Σ,E ∪ Ax) such that Σ is preregular and Ax can include any combination of equational axioms for a function symbol except the id:, left id:, right id:, and assoc without comm.
As explained in Section 12.6.1, if a theory (Σ,E ∪ Ax) satisfies the coherence, confluence, and termination modulo Ax requirements, we can systematically reduce E ∪Ax-unification problems into narrowing problems as follows:
by adding a fresh new sort Truth to Σ with a constant tt;
by transforming each equation in E into a rule in
;
a binary predicate eq
of sort Truth and add to
the rule eq(x,x) → tt, where x has such a top sort;
,Ax,
∪
);
t′ to the narrowing reachability problem
eq(t,t′) →*tt modulo Ax in the rewrite theory (
,Ax,
).That is, we search for all narrowing paths modulo Ax from eq(t,t′) to tt. Each such path then gives us a
unifier of the equation t
t′, just by composing the unifiers of each narrowing step in the narrowing
path.
Therefore, if a theory (Σ,Ax) contains the id:, left id:, or right id: attributes in Ax (but not assoc without comm), we can perform unification modulo Ax as follows:
∪ Ids, where
does not contain any
id:, left id:, or right id: attribute, and Ids is the set of such extra attributes;
,
) where
contains the obvious rules for each of the
equational identity attributes, that is:
, where x is a
variable of sort [s] and e is the identity symbol (if f has also the comm attribute, only
one such rule is needed);
;
,
,
∪
) as above and then reduce an Ax-unification problem t
t′
to the narrowing reachability problem eq(t,t′) →*tt modulo
in the rewrite theory
(
,
,
∪
).However, solving such reachability problems for ACU-unification using full AC-narrowing can easily lead to an infinite search space and therefore to undecidability, since it is well-known that narrowing modulo AC “almost never terminates” and, furthermore, that narrowing strategies facilitating termination such as basic narrowing are incomplete [27, 103]. However, based on the idea of “variants” in [27], a complete, yet quite efficient in terms of its search space, narrowing strategy modulo Ax, called variant narrowing, has been proposed in [53]. Such unification procedure can be used for ACU-unification in an equational theory (Σ,Ax) containing the id:, left id:, or right id: attributes in Ax but not assoc without comm. We proceed as follows (see [53] for further details):
,
) as defined above;
t′, we compute the variants of t and the variants of t′ using
(Σ,
,
) and perform
-unification pairwise among all the variants of t and t′.The unification procedure using variants is implemented through the command id-unify explained below.
Given a functional module, system module, or theory ⟨ModId⟩ having a set Ax of equational axioms for the signature Σ such that Σ does not include symbols with assoc without comm attributes in Ax, the user can give to Full Maude a unification command for Ax-unification of the form:
where only one unification problem is admitted (in contrast to the unify command described in Section 12.4) and such that no limit to the number of unifiers can be specified. The theories supported must satisfy the following requirements:
Consider, for example, the following simple definition of multisets with identity:
We can use the id-unify command to solve the following three unification problems:
For the following substitutions computed for the last unification problem: σ1 = {X:MSet
(a Y:MSet)}
and σ2 = {X:MSet
a,Y:MSet
e}, the reader might believe that σ2 is subsumed by σ1, because the
substitution ρ = {Y:MSet
e} can be built in such a way that σ1;ρ can be normalized into σ2.
However, they are incomparable modulo AC, since both computed substitutions are valid only for
,
-normalized substitutions. Indeed, our equational unification procedure modulo identity can
compute only
,
-normalized unifiers which are valid only for
,
-normalized instance
substitutions. That is, given Ax =
∪ Ids, a unification problem t
t′, and a Ax-unifier
σ,
,
-normalized (if not, we can normalize it and the normalized
version will still be a valid Ax-unifier);
θ;ρ
and both θ and ρ are also assumed to be
,
-normalized.Let us illustrate also the use of left-id and right-id attributes. Consider the following variation of the previous example where we remove associativity and commutativity, but still keep the identity operator as a left identity:
Then the following two unification problems have a different and peculiar meaning, where we have swapped the position of the variables.
Let us now consider the case of a right identity:
Then the very same unification problems above have a different behavior
Narrowing-based reachability analysis is also available at the metalevel by using the function metaNarrowSearch. The functional module providing the necessary infrastructure is called META-NARROWING-SEARCH and the function metaNarrowSearch has the following declaration:
If a non-identity substitution is provided in the fourth argument, then any computed substitution must be an instance of the provided one, i.e., we can restrict the computed narrowing sequences to some concrete shape. The Qid argument metarepresents the appropriate search arrow, similar to the metaSearch command (see Section 11.4.6). For the bounds, the first bound is the maximum length of the narrowing sequences, i.e., the depth of the narrowing tree, and the second bound is the number of computed solutions.
For the NARROWING-VENDING-MACHINE system module in Section 16.4, the following search command considered in that section
can be specified at the metalevel as follows, where ’<_>[’M:Money] is the metarepresentation of the state < M:Money > and ’<_>[’__[’a.Item,’c.Item]] is the metarepresentation of the state < a c >:
Note that we obtain the very same two solutions, where the output contains two terms of type ResultTriple, each one containing the actual output term, its type, and the computed substitution, and
is the metarepresentation of the substitution M:Money --> $ q q q, whereas
is the metarepresentation of the substitution M:Money --> q q q q q q q. Moreover, the substitutions computed in this way also contain all the temporary bindings that narrowing has computed.
The procedure for equational Ax-unification where Ax can contain any equational attribute except assoc without comm is available at the metalevel by using the metaACUUnify function. The functional module providing the necessary infrastructure is called META-E-UNIFICATION and the metaACUUnify function has the following declaration:
For the ID-UNIFICATION-EXAMPLE system module in Section 16.5, the following id-unify command considered in that section
can be specified at the metalevel as follows, where ’__[’a.Elem, ’X:MSet] metarepresents the term a X:MSet and ’__[’__[’a.Elem, ’a.Elem], ’Y:MSet] metarepresents the term a a Y:MSet:
Note that, as expected, the very same two solutions are returned, i.e., the metarepresentations of the two substitutions X:MSet --> a Y:MSet and X:MSet --> a ; Y:MSet --> e.