Inmathematics,computer science, andlogic,rewriting covers a wide range of methods of replacing subterms of aformula with other terms. Such methods may be achieved byrewriting systems (also known asrewrite systems,rewrite engines,[1][2] orreduction systems). In their most basic form, they consist of a set of objects, plusrelations on how to transform those objects.
Rewriting can benon-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 analgorithm 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 ascomputer programs, and severaltheorem provers[3] anddeclarative programming languages are based on term rewriting.[4][5]
Inlogic, the procedure for obtaining theconjunctive normal form (CNF) of a formula can be implemented as a rewriting system.[6] For example, the rules of such a system would be:
For each rule, eachvariable denotes a subexpression, and the symbol () indicates that an expression matching the left hand side of it can be rewritten to one matching the right hand side of it. In such a system, each rule is alogical equivalence, so performing a rewrite on an expression by these rules does not change the truth value of it. Other useful rewriting systems in logic may not preserve truth values, see e.g.equisatisfiability.
Term rewriting systems can be employed to compute arithmetic operations onnatural numbers.To this end, each such number has to be encoded as aterm.The simplest encoding is the one used in thePeano axioms, based on the constant 0 (zero) and thesuccessor functionS. 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.[7]
For example, the computation of 2+2 to result in 4 can be duplicated by term rewriting as follows:
where the notation above each arrow indicates the rule used for each rewrite.
As another example, the computation of 2⋅2 looks like:
where the last step comprises the previous example computation.
Inlinguistics,phrase structure rules, also calledrewrite rules, are used in some systems ofgenerative grammar,[8] as a means of generating the grammatically correct sentences of a language. Such a rule typically takes the form, where A is asyntactic category label, such asnoun phrase orsentence, and X is a sequence of such labels ormorphemes, expressing the fact that A can be replaced by X in generating the constituent structure of a sentence. For example, the rule means that a sentence can consist of a noun phrase (NP) followed by averb phrase (VP); further rules will specify what sub-constituents a noun phrase and a verb phrase can consist of, and so on.
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 anabstract reduction system[9] orabstract rewriting system (abbreviatedARS).[10] An ARS is simply a setA of objects, together with abinary relation → onA called thereduction relation,rewrite relation[11] or justreduction.[9]
Many notions and notations can be defined in the general setting of an ARS. is thereflexive transitive closure of. is thesymmetric closure of. is thereflexive transitive symmetric closure of. Theword problem for an ARS is determining, givenx andy, whether. An objectx inA is calledreducible if there exists some othery inA such that; otherwise it is calledirreducible or anormal form. An objecty is called a "normal form ofx" if, andy is irreducible. If the normal form ofx is unique, then this is usually denoted with. If every object has at least one normal form, the ARS is callednormalizing. orx andy are said to bejoinable if there exists somez with the property that. An ARS is said to possess theChurch–Rosser property if implies. An ARS isconfluent if for allw,x, andy inA, implies. An ARS islocally confluent if and only if for allw,x, andy inA, implies. An ARS is said to beterminating ornoetherian if there is no infinite chain. A confluent and terminating ARS is calledconvergent orcanonical.
Important theorems for abstract rewriting systems are that an ARS isconfluentiff it has the Church–Rosser property,Newman's lemma (a terminating ARS is confluent if and only if it is locally confluent), and that theword problem for an ARS isundecidable in general.
Astring rewriting system (SRS), also known assemi-Thue system, exploits thefree monoid structure of thestrings (words) over analphabet to extend a rewriting relation,, toall strings in the alphabet that contain left- and respectively right-hand sides of some rules assubstrings. Formally a semi-Thue system is atuple where is a (usually finite) alphabet, and is a binary relation between some (fixed) strings in the alphabet, called the set ofrewrite rules. Theone-step rewriting relation induced by on is defined as: if are any strings, then if there exist such that,, and. Since is a relation on, the pair fits the definition of an abstract rewriting system. Since the empty string is in, is a subset of. If the relation issymmetric, then the system is called aThue system.
In a SRS, the reduction relation is compatible with the monoid operation, meaning that implies for all strings. Similarly, the reflexive transitive symmetric closure of, denoted, is acongruence, meaning it is anequivalence relation (by definition) and it is also compatible with string concatenation. The relation is called theThue congruence generated by. In a Thue system, i.e. if is symmetric, the rewrite relation coincides with the Thue congruence.
The notion of a semi-Thue system essentially coincides with thepresentation of a monoid. Since is a congruence, we can define thefactor monoid of the free monoid by the Thue congruence. If a monoid isisomorphic with, then the semi-Thue system is called amonoid presentation of.
We immediately get some very useful connections with other areas of algebra. For example, the alphabet with the rules, where is theempty string, is a presentation of thefree group on one generator. If instead the rules are just, then we obtain a presentation of thebicyclic monoid. Thus semi-Thue systems constitute a natural framework for solving theword problem for monoids and groups. In fact, every monoid has a presentation of the form, 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 thePost–Markov theorem.[12]
Pic.1: Schematic triangle diagram of application of a rewrite rule at position in a term, with matching substitutionPic.2: Rule lhs term matching in term
Aterm rewriting system (TRS) is a rewriting system whose objects areterms, which are expressions with nested sub-expressions. For example, the system shown under§ Logic above is a term rewriting system. The terms in this system are composed of binary operators and and the unary operator. 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 aterm algebra. A term can be visualized as a tree of symbols, the set of admitted symbols being fixed by a givensignature. As a formalism, term rewriting systems have the full power ofTuring machines, that is, everycomputable function can be defined by a term rewriting system.[13]
Some programming languages are based on term rewriting. One such example is Pure, a functional programming language for mathematical applications.[14][15]
"Redex" redirects here. For the medication, seeTadalafil.
Arewrite rule is a pair ofterms, commonly written as, to indicate that the left-hand sidel can be replaced by the right-hand sider. Aterm rewriting system is a setR of such rules. A rule can beapplied to a terms if the left termlmatches somesubterm ofs, that is, if there is somesubstitution such that the subterm of rooted at somepositionp is the result of applying the substitution to the terml. The subterm matching the left hand side of the rule is called aredex orreducible expression.[16] The result termt of this rule application is then the result ofreplacing the subterm at positionp ins by the term with the substitution applied, see picture 1. In this case, is said to berewritten in one step, orrewritten directly, to by the system, formally denoted as,, or as by some authors.
If a term can be rewritten in several steps into a term, that is, if, the term is said to berewritten to, formally denoted as. In other words, the relation is thetransitive closure of the relation; often, also the notation is used to denote thereflexive-transitive closure of, that is, if or.[17] A term rewriting given by a set of rules can be viewed as an abstract rewriting system as definedabove, with terms as its objects and as its rewrite relation.
For example, 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 with the matching substitution, see picture 2.[note 2] Applying that substitution to the rule's right-hand side yields the term, and replacing the numerator by that term yields, 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" in elementary algebra. Alternately, the rule could have been applied to the denominator of the original term, yielding.
Termination issues of rewrite systems in general are handled inAbstract 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 alinear left-hand side is undecidable.[18][19] Termination is also undecidable for systems using only unary function symbols; however, it is decidable for finiteground systems.[20]
The following term rewrite system is normalizing,[note 3] but not terminating,[note 4] and not confluent:[21]
The following two examples of terminating term rewrite systems are due to Toyama:[22]
and
Their union is a non-terminating system, since
This result disproves a conjecture ofDershowitz,[23] who claimed that the union of two terminating term rewrite systems and is again terminating if all left-hand sides of and right-hand sides of arelinear, and there are no "overlaps" between left-hand sides of and right-hand sides of. All these properties are satisfied by Toyama's examples.
Higher-order rewriting systems are a generalization of first-order term rewriting systems tolambda terms, allowing higher order functions and bound variables.[24] Various results about first-order TRSs can be reformulated for HRSs as well.[25]
Trace theory provides a means for discussing multiprocessing in more formal terms, such as via thetrace monoid and thehistory monoid. Rewriting can be performed in trace systems as well.
^This variant of the previous rule is needed since the commutative lawA∨B =B∨A cannot be turned into a rewrite rule. A rule likeA∨B →B∨A would cause the rewrite system to be nonterminating.
^since applying that substitution to the rule's left hand side yields the numerator
^i.e. for each term, some normal form exists, e.g.h(c,c) has the normal formsb andg(b), sinceh(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, andh(c,c) →f(h(c,c),h(c,c)) →g(h(c,c)) → ... →g(b); neitherb norg(b) can be rewritten any further, therefore the system is not confluent
^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)) → ...
Marc Bezem,Jan Willem Klop,Roel de Vrijer ("Terese"),Term Rewriting Systems ("TeReSe"), Cambridge University Press, 2003,ISBN0-521-39115-6. This is the most recent comprehensive monograph. It uses however a fair deal of non-yet-standard notations and definitions. For instance, the Church–Rosser property is defined to be identical with confluence.
Jürgen Avenhaus and Klaus Madlener. "Term rewriting and equational reasoning". In Ranan B. Banerji (Ed.),Formal Techniques in Artificial Intelligence: A Sourcebook, Elsevier (1990).
String rewriting
Ronald V. Book and Friedrich Otto,String-Rewriting Systems, Springer (1993).
Benjamin Benninghofen, Susanne Kemmerich andMichael M. Richter,Systems of Reductions.LNCS277, Springer-Verlag (1987).
^Jürgen Avenhaus; Klaus Madlener (1990). "Term Rewriting and Equational Reasoning". In R.B. Banerji (ed.).Formal Techniques in Artificial Intelligence. Sourcebook. Elsevier. pp. 1–43. Here: Example in sect.4.1, p.24.
^Klop, J. W."Term Rewriting Systems"(PDF).Papers by Nachum Dershowitz and students. Tel Aviv University. p. 12.Archived(PDF) from the original on 15 August 2021. Retrieved14 August 2021.
^N. Dershowitz,J.-P. Jouannaud (1990).Jan van Leeuwen (ed.).Rewrite Systems. Handbook of Theoretical Computer Science. Vol. B. Elsevier. pp. 243–320.; here: Sect. 2.3
^Max Dauchet (1989). "Simulation of Turing Machines by a Left-Linear Rewrite Rule".Proc. 3rd Int. Conf. onRewriting Techniques and Applications. LNCS. Vol. 355. Springer. pp. 109–120.
^N. Dershowitz (1985)."Termination"(PDF). InJean-Pierre Jouannaud (ed.).Proc. RTA. LNCS. Vol. 220. Springer. pp. 180–224.Archived(PDF) from the original on 2013-11-12. Retrieved2013-06-16.; here: p.210
^Nipkow, Tobias; Prehofer, Christian (1998)."Higher-Order Rewriting and Equational Reasoning". In Bibel, W.; Schmitt, P. (eds.).Automated Deduction - A Basis for Applications. Volume I: Foundations. Kluwer. pp. 399–430.Archived from the original on 2021-08-16. Retrieved2021-08-16.