A
term rewriting system (
TRS) is a rewriting system whose objects are
terms, which are expressions with nested sub-expressions. For example, the system shown under '''' above is a term rewriting system. The terms in this system are composed of binary operators (\vee) and (\wedge) and the unary operator (\neg). Also present in the rules are variables, which represent any possible term (though a single variable always represents the same term throughout a single rule). In contrast to string rewriting systems, whose objects are sequences of symbols, the objects of a term rewriting system form a
term algebra. A term can be visualized as a tree of symbols, the set of admitted symbols being fixed by a given
signature. As a formalism, term rewriting systems have the full power of
Turing machines, that is, every
computable function can be defined by a term rewriting system. Some programming languages are based on term rewriting. One such example is Pure, a functional programming language for mathematical applications.
Formal definition A
rewrite rule is a pair of
terms, commonly written as l \rightarrow r, to indicate that the left-hand side can be replaced by the right-hand side . A
term rewriting system is a set of such rules. A rule l \rightarrow r can be
applied to a term if the left term
matches some
subterm of , that is, if there is some
substitution \sigma such that the subterm of s rooted at some
position is the result of applying the substitution \sigma to the term . The subterm matching the left hand side of the rule is called a
redex or
reducible expression. The result term of this rule application is then the result of
replacing the subterm at position in by the term r with the substitution \sigma applied, see picture 1. In this case, s is said to be
rewritten in one step, or
rewritten directly, to t by the system R, formally denoted as s \rightarrow_R t, s \underset{R}\rightarrow t, or as s \overset{R}\rightarrow t by some authors. If a term t_1 can be rewritten in several steps into a term t_n, that is, if t_1 \underset{R}\rightarrow t_2 \underset{R}\rightarrow \cdots \underset{R}\rightarrow t_n, the term t_1 is said to be
rewritten to t_n, formally denoted as t_1 \overset{+}\underset{R}\rightarrow t_n. In other words, the relation \overset{+}\underset{R}\rightarrow is the
transitive closure of the relation \underset{R}\rightarrow; often, also the notation \overset{*}\underset{R}\rightarrow is used to denote the
reflexive-transitive closure of \underset{R}\rightarrow, that is, s \overset{*}\underset{R}\rightarrow t if s = t or {{nobreak|s \overset{+}\underset{R}\rightarrow t.}} A term rewriting given by a set R of rules can be viewed as an abstract rewriting system as defined
above, with terms as its objects and \underset{R}\rightarrow as its rewrite relation. For example, x*(y*z) \rightarrow (x*y)*z is a rewrite rule, commonly used to establish a normal form with respect to the associativity of *. That rule can be applied at the numerator in the term \frac{a*((a+1)*(a+2))}{1*(2*3)} with the matching substitution \{ x \mapsto a, \; y \mapsto a+1, \; z \mapsto a+2 \}, see picture 2. Applying that substitution to the rule's right-hand side yields the term (a*(a+1))*(a+2), and replacing the numerator by that term yields \frac{(a*(a+1))*(a+2)}{1*(2*3)}, which is the result term of applying the rewrite rule. Altogether, applying the rewrite rule has achieved what is called "applying the associativity law for * to \frac{a*((a+1)*(a+2))}{1*(2*3)}" in elementary algebra. Alternately, the rule could have been applied to the denominator of the original term, yielding \frac{a*((a+1)*(a+2))}{(1*2)*3}.
Termination Termination issues of rewrite systems in general are handled in
Abstract rewriting system#Termination and convergence. For term rewriting systems in particular, the following additional subtleties are to be considered. Termination even of a system consisting of one rule with a
linear left-hand side is undecidable. Termination is also undecidable for systems using only unary function symbols; however, it is decidable for finite
ground systems. The following term rewrite system is normalizing, but not terminating, and not confluent: \begin{align} f(x,x) & \rightarrow g(x) , \\ f(x,g(x)) & \rightarrow b , \\ h(c,x) & \rightarrow f(h(x,c),h(x,x)) . \\ \end{align} The following two examples of terminating term rewrite systems are due to Toyama: :f(0,1,x) \rightarrow f(x,x,x) and :g(x,y) \rightarrow x, :g(x,y) \rightarrow y. Their union is a non-terminating system, since \begin{align} & f(g(0,1),g(0,1),g(0,1)) \\ \rightarrow & f(0,g(0,1),g(0,1)) \\ \rightarrow & f(0,1,g(0,1)) \\ \rightarrow & f(g(0,1),g(0,1),g(0,1)) \\ \rightarrow & \cdots \end{align} This result disproves a conjecture of
Dershowitz, who claimed that the union of two terminating term rewrite systems R_1 and R_2 is again terminating if all left-hand sides of R_1 and right-hand sides of R_2 are
linear, and there are no "
overlaps" between left-hand sides of R_1 and right-hand sides of R_2. All these properties are satisfied by Toyama's examples. See
Rewrite order and
Path ordering (term rewriting) for ordering relations used in termination proofs for term rewriting systems.
Higher-order rewriting systems Higher-order rewriting systems are a generalization of first-order term rewriting systems to
lambda terms, allowing higher order functions and bound variables. Various results about first-order TRSs can be reformulated for HRSs as well.
Graph rewriting systems Graph rewrite systems are another generalization of term rewrite systems, operating on
graphs instead of (
ground-)
terms / their corresponding
tree representation. == Trace rewriting systems ==