Rewriting Explained

In mathematics, computer science, and logic, rewriting covers a wide range of methods of replacing subterms of a formula with other terms. Such methods may be achieved by rewriting systems (also known as rewrite systems, rewrite engines,[1] [2] or reduction systems). In their most basic form, they consist of a set of objects, plus relations on how to transform those objects.

Rewriting can be non-deterministic. One rule to rewrite a term could be applied in many different ways to that term, or more than one rule could be applicable. Rewriting systems then do not provide an algorithm for changing one term to another, but a set of possible rule applications. When combined with an appropriate algorithm, however, rewrite systems can be viewed as computer programs, and several theorem provers[3] and declarative programming languages are based on term rewriting.[4] [5]

Example cases

Logic

In logic, the procedure for obtaining the conjunctive normal form (CNF) of a formula can be implemented as a rewriting system.[6] The rules of an example of such a system would be:

\neg\negA\toA

(double negation elimination)

\neg(A\landB)\to\negA\lor\negB

(De Morgan's laws)

\neg(A\lorB)\to\negA\land\negB

(A\landB)\lorC\to(A\lorC)\land(B\lorC)

(distributivity)

A\lor(B\landC)\to(A\lorB)\land(A\lorC),

[7] where the symbol (

\to

) indicates that an expression matching the left hand side of the rule can be rewritten to one formed by the right hand side, and the symbols each denote a subexpression. In such a system, each rule is chosen so that the left side is equivalent to the right side, and consequently when the left side matches a subexpression, performing a rewrite of that subexpression from left to right maintains logical consistency and value of the entire expression.

Arithmetic

Term rewriting systems can be employed to compute arithmetic operations on natural numbers.To this end, each such number has to be encoded as a term.The simplest encoding is the one used in the Peano axioms, based on the constant 0 (zero) and the successor function S. For example, the numbers 0, 1, 2, and 3 are represented by the terms 0, S(0), S(S(0)), and S(S(S(0))), respectively.The following term rewriting system can then be used to compute sum and product of given natural numbers.[8]

\begin{align} A+0&\toA&rm{(1)},\\ A+S(B)&\toS(A+B)&rm{(2)},\\ A0&\to0&rm{(3)},\\ AS(B)&\toA+(AB)&rm{(4)}. \end{align}

For example, the computation of 2+2 to result in 4 can be duplicated by term rewriting as follows:

S(S(0))+S(S(0))

  \stackrel{(2)}{\to}  

S(S(S(0))+S(0))

  \stackrel{(2)}{\to}  

S(S(S(S(0))+0))

  \stackrel{(1)}{\to}  

S(S(S(S(0)))),

where the rule numbers are given above the rewrites-to arrow.

As another example, the computation of 2⋅2 looks like:

S(S(0))S(S(0))

  \stackrel{(4)}{\to}  

S(S(0))+S(S(0))S(0)

  \stackrel{(4)}{\to}  

S(S(0))+S(S(0))+S(S(0))0

  \stackrel{(3)}{\to}  

S(S(0))+S(S(0))+0

  \stackrel{(1)}{\to}  

S(S(0))+S(S(0))

  \stackrel{rm{s.a.}}{\to}  

S(S(S(S(0)))),

where the last step comprises the previous example computation.

Linguistics

In linguistics, phrase structure rules, also called rewrite rules, are used in some systems of generative grammar,[9] as a means of generating the grammatically correct sentences of a language. Such a rule typically takes the form

\rmAX

, where A is a syntactic category label, such as noun phrase or sentence, and X is a sequence of such labels or morphemes, expressing the fact that A can be replaced by X in generating the constituent structure of a sentence. For example, the rule

\rmSNPVP

means that a sentence can consist of a noun phrase (NP) followed by a verb phrase (VP); further rules will specify what sub-constituents a noun phrase and a verb phrase can consist of, and so on.

Abstract rewriting systems

See main article: Abstract rewriting system.

From the above examples, it is clear that we can think of rewriting systems in an abstract manner. We need to specify a set of objects and the rules that can be applied to transform them. The most general (unidimensional) setting of this notion is called an abstract reduction system[10] or abstract rewriting system (abbreviated ARS).[11] An ARS is simply a set A of objects, together with a binary relation → on A called the reduction relation, rewrite relation[12] or just reduction.[10]

Many notions and notations can be defined in the general setting of an ARS.

\overset{*}

is the reflexive transitive closure of

.

\leftrightarrow

is the symmetric closure of

.

\overset{*}{\leftrightarrow}

is the reflexive transitive symmetric closure of

. The word problem for an ARS is determining, given x and y, whether

x\overset{*}{\leftrightarrow}y

. An object x in A is called reducible if there exists some other y in A such that

xy

; otherwise it is called irreducible or a normal form. An object y is called a "normal form of x" if

x\stackrel{*}{}y

, and y is irreducible. If the normal form of x is unique, then this is usually denoted with

x{\downarrow}

. If every object has at least one normal form, the ARS is called normalizing.

x\downarrowy

or x and y are said to be joinable if there exists some z with the property that

x\overset{*}{}z\overset{*}{\leftarrow}y

. An ARS is said to possess the Church–Rosser property if

x\overset{*}{\leftrightarrow}y

implies

x\downarrowy

. An ARS is confluent if for all w, x, and y in A,

x\overset{*}{\leftarrow}w\overset{*}{}y

implies

x\downarrowy

. An ARS is locally confluent if and only if for all w, x, and y in A,

x\leftarrowwy

implies

xn\downarrowy

. An ARS is said to be terminating or noetherian if there is no infinite chain

x0x1x2

. A confluent and terminating ARS is called convergent or canonical.

Important theorems for abstract rewriting systems are that an ARS is confluent iff it has the Church–Rosser property, Newman's lemma (a terminating ARS is confluent if and only if it is locally confluent), and that the word problem for an ARS is undecidable in general.

String rewriting systems

See main article: String rewriting system.

A string rewriting system (SRS), also known as semi-Thue system, exploits the free monoid structure of the strings (words) over an alphabet to extend a rewriting relation,

R

, to all strings in the alphabet that contain left- and respectively right-hand sides of some rules as substrings. Formally a semi-Thue system is a tuple

(\Sigma,R)

where

\Sigma

is a (usually finite) alphabet, and

R

is a binary relation between some (fixed) strings in the alphabet, called the set of rewrite rules. The one-step rewriting relation

\underset{R}

induced by

R

on

\Sigma*

is defined as: if

s,t\in\Sigma*

are any strings, then

s\underset{R}t

if there exist

x,y,u,v\in\Sigma*

such that

s=xuy

,

t=xvy

, and

uRv

. Since

\underset{R}

is a relation on

\Sigma*

, the pair

(\Sigma*,\underset{R})

fits the definition of an abstract rewriting system. Since the empty string is in

\Sigma*

,

R

is a subset of

\underset{R}

. If the relation

R

is symmetric, then the system is called a Thue system.

In a SRS, the reduction relation

\overset{*}\underset{R}

is compatible with the monoid operation, meaning that

x\overset{*}\underset{R}y

implies

uxv\overset{*}\underset{R}uyv

for all strings

x,y,u,v\in\Sigma*

. Similarly, the reflexive transitive symmetric closure of

\underset{R}

, denoted

\overset{*}{\undersetR\leftrightarrow}

, is a congruence, meaning it is an equivalence relation (by definition) and it is also compatible with string concatenation. The relation

\overset{*}\underset{R}\leftrightarrow

is called the Thue congruence generated by

R

. In a Thue system, i.e. if

R

is symmetric, the rewrite relation

\overset{*}\underset{R}

coincides with the Thue congruence

\overset{*}{\undersetR\leftrightarrow}

.

The notion of a semi-Thue system essentially coincides with the presentation of a monoid. Since

\overset{*}{\undersetR\leftrightarrow}

is a congruence, we can define the factor monoid

l{M}R=\Sigma*/\overset{*}{\undersetR\leftrightarrow}

of the free monoid

\Sigma*

by the Thue congruence. If a monoid

l{M}

is isomorphic with

l{M}R

, then the semi-Thue system

(\Sigma,R)

is called a monoid presentation of

l{M}

.

We immediately get some very useful connections with other areas of algebra. For example, the alphabet

\{a,b\}

with the rules

\{ab\varepsilon,ba\varepsilon\}

, where

\varepsilon

is the empty string, is a presentation of the free group on one generator. If instead the rules are just

\{ab\varepsilon\}

, then we obtain a presentation of the bicyclic monoid. Thus semi-Thue systems constitute a natural framework for solving the word problem for monoids and groups. In fact, every monoid has a presentation of the form

(\Sigma,R)

, i.e. it may always be presented by a semi-Thue system, possibly over an infinite alphabet.

The word problem for a semi-Thue system is undecidable in general; this result is sometimes known as the Post–Markov theorem.[13]

Term rewriting systems

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.[14]

Formal definition

A rewrite rule is a pair of terms, commonly written as

lr

, 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

lr

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.[15] 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

sRt

,

s\underset{R}t

, or as

s\overset{R}t

by some authors.

If a term

t1

can be rewritten in several steps into a term

tn

, that is, if

t1\underset{R}t2\underset{R}\underset{R}tn

, the term

t1

is said to be rewritten to

tn

, formally denoted as

t1\overset{+}\underset{R}tn

. In other words, the relation

\overset{+}\underset{R}

is the transitive closure of the relation

\underset{R}

; often, also the notation

\overset{*}\underset{R}

is used to denote the reflexive-transitive closure of

\underset{R}

, that is,

s\overset{*}\underset{R}t

if

s=t

or 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}

as its rewrite relation.

For example,

x*(y*z)(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
a*((a+1)*(a+2))
1*(2*3)
with the matching substitution

\{x\mapstoa,y\mapstoa+1,z\mapstoa+2\}

, see picture 2.[16] 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
(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
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
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.[17] [18] Termination is also undecidable for systems using only unary function symbols; however, it is decidable for finite ground systems.[19]

The following term rewrite system is normalizing,[20] but not terminating,[21] and not confluent:[22] \beginf(x,x) & \rightarrow g(x), \\f(x,g(x)) & \rightarrow b, \\h(c,x) & \rightarrow f(h(x,c),h(x,x)) . \\\end

The following two examples of terminating term rewrite systems are due to Toyama:[23]

f(0,1,x)f(x,x,x)

and

g(x,y)x,

g(x,y)y.

Their union is a non-terminating system, since

\begin& 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\endThis result disproves a conjecture of Dershowitz,[24] who claimed that the union of two terminating term rewrite systems

R1

and

R2

is again terminating if all left-hand sides of

R1

and right-hand sides of

R2

are linear, and there are no "overlaps" between left-hand sides of

R1

and right-hand sides of

R2

. 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.[25] Various results about first-order TRSs can be reformulated for HRSs as well.[26]

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

Trace theory provides a means for discussing multiprocessing in more formal terms, such as via the trace monoid and the history monoid. Rewriting can be performed in trace systems as well.

See also

Further reading

String rewriting
Other

External links

Notes and References

  1. [Joseph Goguen]
  2. Sculthorpe . Neil . Frisby . Nicolas . Gill . Andy . The Kansas University rewrite engine . . 24 . 4 . 2014 . 434–473 . 0956-7968 . 10.1017/S0956796814000185 . 16807490 . 2019-02-12 . 2017-09-22 . https://web.archive.org/web/20170922234803/http://irep.ntu.ac.uk/id/eprint/27901/1/PubSub5454_Sculthorpe.pdf . live .
  3. 10.1016/0743-1066(92)90047-7. The term rewriting approach to automated theorem proving. The Journal of Logic Programming. 14. 1–2. 71–99. 1992. Hsiang. Jieh. Kirchner. Hélène. Lescanne. Pierre. Rusinowitch. Michaël. free.
  4. 10.1016/S0743-1066(98)10005-5 . Theory and practice of constraint handling rules . The Journal of Logic Programming . 37 . 1–3 . 95–138 . 1998 . Frühwirth . Thom . free .
  5. 10.1016/S0304-3975(01)00359-0. Maude: Specification and programming in rewriting logic. Theoretical Computer Science. 285. 2. 187–243. 2002. Clavel. M.. Durán. F.. Eker. S.. Lincoln. P.. Martí-Oliet. N.. Meseguer. J.. Quesada. J.F.. free.
  6. Book: Kim Marriott. Peter J. Stuckey. Programming with Constraints: An Introduction. 1998. MIT Press. 978-0-262-13341-8. 436–.
  7. This variant of the previous rule is needed since the commutative law AB = BA cannot be turned into a rewrite rule. A rule like ABBA would cause the rewrite system to be nonterminating.
  8. Book: Jürgen Avenhaus . Klaus Madlener . Term Rewriting and Equational Reasoning . 1 - 43 . Formal Techniques in Artificial Intelligence . R.B. Banerji . Elsevier . Sourcebook . 1990 . Here: Example in sect.4.1, p.24.
  9. Book: Robert Freidin. Foundations of Generative Syntax. 1992. MIT Press. 978-0-262-06144-5.
  10. Book and Otto, p. 10
  11. Bezem et al., p. 7,
  12. Bezem et al., p. 7
  13. Martin Davis et al. 1994, p. 178
  14. Dershowitz, Jouannaud (1990), sect.1, p.245
  15. Web site: Klop . J. W. . Term Rewriting Systems . Papers by Nachum Dershowitz and students . Tel Aviv University . 14 August 2021 . 12 . 15 August 2021 . https://web.archive.org/web/20210815025906/http://www.cs.tau.ac.il/~nachum/papers/klop.pdf . live .
  16. since applying that substitution to the rule's left hand side

    x*(y*z)

    yields the numerator

    a*((a+1)*(a+2))

  17. Book: Max Dauchet. Simulation of Turing Machines by a Left-Linear Rewrite Rule. Proc. 3rd Int. Conf. on Rewriting Techniques and Applications. 1989. 355. 109–120. Springer. LNCS.
  18. Max Dauchet . Simulation of Turing machines by a regular rewrite rule . . 103 . 2 . 409 - 420 . Sep 1992 . 10.1016/0304-3975(92)90022-8 . free .
  19. Gerard Huet, D.S. Lankford. On the Uniform Halting Problem for Term Rewriting Systems. Mar 1978. 283. 8. IRIA. 16 June 2013.
  20. i.e. for each term, some normal form exists, e.g. h(c,c) has the normal forms b and g(b), since h(c,c) → f(h(c,c),h(c,c)) → f(h(c,c),f(h(c,c),h(c,c))) → f(h(c,c),g(h(c,c))) → b, and h(c,c) → f(h(c,c),h(c,c)) → g(h(c,c)) → ... → g(b); neither b nor g(b) can be rewritten any further, therefore the system is not confluent
  21. i.e., there are infinite derivations, e.g. h(c,c) → f(h(c,c),h(c,c)) → f(f(h(c,c),h(c,c)),h(c,c)) → f(f(f(h(c,c),h(c,c)),h(c,c)),h(c,c)) → ...
  22. Book: Bernhard Gramlich. Relating Innermost, Weak, Uniform, and Modular Termination of Term Rewriting Systems. Proc. International Conference on Logic Programming and Automated Reasoning (LPAR). Jun 1993. 624. 285–296. Springer. Voronkov, Andrei. LNAI. http://www.logic.at/staff/gramlich/papers/lpar92.ps.gz. International Conference on Logic Programming and Automated Reasoning. 2014-06-19. 2016-03-04. https://web.archive.org/web/20160304185714/http://www.logic.at/staff/gramlich/papers/lpar92.ps.gz. live. Here: Example 3.3
  23. Yoshihito Toyama. Counterexamples to Termination for the Direct Sum of Term Rewriting Systems. Inf. Process. Lett.. 1987. 25. 3. 141–143. 10.1016/0020-0190(87)90122-0. 2433/99946. free. 2019-11-13. 2019-11-13. https://web.archive.org/web/20191113091157/http://www.nue.ie.niigata-u.ac.jp/toyama/user/toyama/research/paper/journal/counterexamples.pdf. live.
  24. Book: N. Dershowitz. Termination. Proc. RTA. 1985. 220. 180–224. Springer. Jean-Pierre Jouannaud. Jean-Pierre Jouannaud. LNCS. http://www.cs.tau.ac.il/~nachum/papers/LNCS/Termination.pdf. 2013-06-16. 2013-11-12. https://web.archive.org/web/20131112200935/http://www.cs.tau.ac.il/~nachum/papers/LNCS/Termination.pdf. live.
    here: p.210
  25. Book: D. A. . Wolfram . The Clausal Theory of Types . 1993 . Cambridge University Press . 47–50 . 10.1017/CBO9780511569906. 9780521395380 . 42331173 .
  26. Book: Nipkow . Tobias . Prehofer . Christian . Bibel . W. . Schmitt . P. . Automated Deduction - A Basis for Applications. Volume I: Foundations . 1998 . Kluwer . 399–430 . Higher-Order Rewriting and Equational Reasoning . https://www21.in.tum.de/~nipkow/pubs/ded-book.html . 2021-08-16 . 2021-08-16 . https://web.archive.org/web/20210816153129/https://www21.in.tum.de/~nipkow/pubs/ded-book.html . live .