Jump to content

Rewriting

From Wikipedia, the free encyclopedia

Inmathematics,computer science,andlogic,rewritingcovers a wide range of methods of replacing subterms of aformulawith 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, plus relations 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 analgorithmfor 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 languagesare based on term rewriting.[4][5]

Example cases

[edit]

Logic

[edit]

Inlogic,the procedure for obtaining theconjunctive 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:

(double negation elimination)
(De Morgan's laws)
(distributivity)
[note 1]

where the symbol () 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 isequivalentto 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

[edit]

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 rule numbers are given above therewrites-toarrow.

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

where the last step comprises the previous example computation.

Linguistics

[edit]

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 categorylabel, such asnoun phraseorsentence,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 rulemeans 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.

Abstract rewriting systems

[edit]

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 setAof objects, together with abinary relation→ onAcalled 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 closureof.is thesymmetric closureof.is thereflexive transitive symmetric closureof.Theword problemfor an ARS is determining, givenxandy,whether.An objectxinAis calledreducibleif there exists some otheryinAsuch that;otherwise it is calledirreducibleor anormal form.An objectyis called a "normal form ofx"if,andyis irreducible. If the normal form ofxis unique, then this is usually denoted with.If every object has at least one normal form, the ARS is callednormalizing.orxandyare said to bejoinableif there exists somezwith the property that.An ARS is said to possess theChurch–Rosser propertyifimplies.An ARS isconfluentif for allw,x,andyinA,implies.An ARS islocally confluentif and only if for allw,x,andyinA,implies.An ARS is said to beterminatingornoetherianif there is no infinite chain.A confluent and terminating ARS is calledconvergentorcanonical.

Important theorems for abstract rewriting systems are that an ARS isconfluentiffit has the Church–Rosser property,Newman's lemma(a terminating ARS is confluent if and only if it is locally confluent), and that theword problemfor an ARS isundecidablein general.

String rewriting systems

[edit]

Astring rewriting system(SRS), also known assemi-Thue system,exploits thefree monoidstructure of thestrings(words) over analphabetto extend a rewriting relation,,toallstrings in the alphabet that contain left- and respectively right-hand sides of some rules assubstrings.Formally a semi-Thue system is atuplewhereis a (usually finite) alphabet, andis a binary relation between some (fixed) strings in the alphabet, called the set ofrewrite rules.Theone-step rewriting relationinduced byonis defined as: ifare any strings, thenif there existsuch that,,and.Sinceis a relation on,the pairfits the definition of an abstract rewriting system. Since the empty string is in,is a subset of.If the relationissymmetric,then the system is called aThue system.

In a SRS, the reduction relationis compatible with the monoid operation, meaning thatimpliesfor 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 relationis called theThue congruencegenerated by.In a Thue system, i.e. ifis symmetric, the rewrite relationcoincides with the Thue congruence.

The notion of a semi-Thue system essentially coincides with thepresentation of a monoid.Sinceis a congruence, we can define thefactor monoidof the free monoidby the Thue congruence. If a monoidisisomorphicwith,then the semi-Thue systemis called amonoid presentationof.

We immediately get some very useful connections with other areas of algebra. For example, the alphabetwith the rules,whereis theempty string,is a presentation of thefree groupon 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 problemfor 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]

Term rewriting systems

[edit]
Pic.1:Schematic triangle diagram of application of a rewrite ruleat positionin a term, with matching substitution
Pic.2:Rule lhs termmatching 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§ Logicabove is a term rewriting system. The terms in this system are composed of binary operatorsandand 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 functioncan be defined by a term rewriting system.[13]

Formal definition

[edit]

Arewrite ruleis a pair ofterms,commonly written as,to indicate that the left-hand sidelcan be replaced by the right-hand sider.Aterm rewriting systemis a setRof such rules. A rulecan beappliedto a termsif the left termlmatchessomesubtermofs,that is, if there is somesubstitutionsuch that the subterm ofrooted at somepositionpis the result of applying the substitutionto the terml.The subterm matching the left hand side of the rule is called aredexorreducible expression.[14]The result termtof this rule application is then the result ofreplacing the subtermat positionpinsby the termwith the substitutionapplied, see picture 1. In this case,is said to berewritten in one step,orrewritten directly,toby the system,formally denoted as,,or asby some authors.

If a termcan be rewritten in several steps into a term,that is, if,the termis said to berewrittento,formally denoted as.In other words, the relationis thetransitive closureof the relation;often, also the notationis used to denote thereflexive-transitive closureof,that is,ifor.[15]A term rewriting given by a setof rules can be viewed as an abstract rewriting system as definedabove,with terms as its objects andas 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 termwith 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 forto"in elementary algebra. Alternately, the rule could have been applied to the denominator of the original term, yielding.

Termination

[edit]

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 alinearleft-hand side is undecidable.[16][17]Termination is also undecidable for systems using only unary function symbols; however, it is decidable for finitegroundsystems.[18]

The following term rewrite system is normalizing,[note 3]but not terminating,[note 4]and not confluent:[19]

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

and

Their union is a non-terminating system, since

This result disproves a conjecture ofDershowitz,[21]who claimed that the union of two terminating term rewrite systemsandis again terminating if all left-hand sides ofand right-hand sides ofarelinear,and there are no "overlaps"between left-hand sides ofand right-hand sides of.All these properties are satisfied by Toyama's examples.

SeeRewrite orderandPath ordering (term rewriting)for ordering relations used in termination proofs for term rewriting systems.

Higher-order rewriting systems

[edit]

Higher-order rewriting systems are a generalization of first-order term rewriting systems tolambda terms,allowing higher order functions and bound variables.[22]Various results about first-order TRSs can be reformulated for HRSs as well.[23]

Graph rewriting systems

[edit]

Graph rewrite systemsare another generalization of term rewrite systems, operating ongraphsinstead of (ground-)terms/ their correspondingtreerepresentation.

Trace rewriting systems

[edit]

Trace theoryprovides a means for discussing multiprocessing in more formal terms, such as via thetrace monoidand thehistory monoid.Rewriting can be performed in trace systems as well.

See also

[edit]

Notes

[edit]
  1. ^This variant of the previous rule is needed since the commutative lawAB=BAcannot be turned into a rewrite rule. A rule likeABBAwould cause the rewrite system to be nonterminating.
  2. ^since applying that substitution to the rule's left hand sideyields the numerator
  3. ^i.e. for each term, some normal form exists, e.g.h(c,c) has the normal formsbandg(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); neitherbnorg(b) can be rewritten any further, therefore the system is not confluent
  4. ^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)) →...

Further reading

[edit]
  • Baader, Franz;Nipkow, Tobias(1999).Term rewriting and all that.Cambridge University Press.ISBN978-0-521-77920-3.316 pages.
  • 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.
  • Nachum DershowitzandJean-Pierre Jouannaud"Rewrite Systems",Chapter 6 inJan van Leeuwen(Ed.),Handbook of Theoretical Computer Science,Volume B: Formal Models and Semantics.,Elsevier and MIT Press, 1990,ISBN0-444-88074-7,pp. 243–320. Thepreprintof this chapter is freely available from the authors, but it is missing the figures.
  • Nachum Dershowitz andDavid Plaisted."Rewriting",Chapter 9 inJohn Alan RobinsonandAndrei Voronkov(Eds.),Handbook of Automated Reasoning,Volume 1.
  • Gérard Huetet Derek Oppen,Equations and Rewrite Rules, A Survey(1980) Stanford Verification Group, Report N° 15 Computer Science Department Report N° STAN-CS-80-785
  • Jan Willem Klop."Term Rewriting Systems", Chapter 1 inSamson Abramsky,Dov M. GabbayandTom Maibaum(Eds.),Handbook of Logic in Computer Science,Volume 2: Background: Computational Structures.
  • David Plaisted."Equational reasoning and term rewriting systems",inDov M. Gabbay,C. J. HoggerandJohn Alan Robinson(Eds.),Handbook of Logic in Artificial Intelligence and Logic Programming,Volume 1.
  • 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. Bookand Friedrich Otto,String-Rewriting Systems,Springer (1993).
  • Benjamin Benninghofen, Susanne Kemmerich andMichael M. Richter,Systems of Reductions.LNCS277,Springer-Verlag (1987).
Other
[edit]

References

[edit]
  1. ^ Joseph Goguen"Proving and Rewriting" International Conference on Algebraic and Logic Programming, 1990 Nancy, France pp 1-24
  2. ^Sculthorpe, Neil; Frisby, Nicolas; Gill, Andy (2014)."The Kansas University rewrite engine"(PDF).Journal of Functional Programming.24(4): 434–473.doi:10.1017/S0956796814000185.ISSN0956-7968.S2CID16807490.Archived(PDF)from the original on 2017-09-22.Retrieved2019-02-12.
  3. ^Hsiang, Jieh; Kirchner, Hélène; Lescanne, Pierre; Rusinowitch, Michaël (1992)."The term rewriting approach to automated theorem proving".The Journal of Logic Programming.14(1–2): 71–99.doi:10.1016/0743-1066(92)90047-7.
  4. ^Frühwirth, Thom (1998)."Theory and practice of constraint handling rules".The Journal of Logic Programming.37(1–3): 95–138.doi:10.1016/S0743-1066(98)10005-5.
  5. ^abClavel, M.; Durán, F.; Eker, S.; Lincoln, P.; Martí-Oliet, N.; Meseguer, J.; Quesada, J.F. (2002)."Maude: Specification and programming in rewriting logic".Theoretical Computer Science.285(2): 187–243.doi:10.1016/S0304-3975(01)00359-0.
  6. ^Kim Marriott; Peter J. Stuckey (1998).Programming with Constraints: An Introduction.MIT Press. pp. 436–.ISBN978-0-262-13341-8.
  7. ^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.
  8. ^Robert Freidin (1992).Foundations of Generative Syntax.MIT Press.ISBN978-0-262-06144-5.
  9. ^abBook and Otto, p. 10
  10. ^Bezem et al., p. 7,
  11. ^Bezem et al., p. 7
  12. ^Martin Davis et al. 1994, p. 178
  13. ^Dershowitz, Jouannaud (1990), sect.1, p.245
  14. ^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 August2021.
  15. ^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
  16. ^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.
  17. ^Max Dauchet (Sep 1992)."Simulation of Turing machines by a regular rewrite rule".Theoretical Computer Science.103(2): 409–420.doi:10.1016/0304-3975(92)90022-8.
  18. ^Gerard Huet, D.S. Lankford (Mar 1978).On the Uniform Halting Problem for Term Rewriting Systems(PDF)(Technical report). IRIA. p. 8. 283.Retrieved16 June2013.
  19. ^Bernhard Gramlich (Jun 1993)."Relating Innermost, Weak, Uniform, and Modular Termination of Term Rewriting Systems".In Voronkov, Andrei (ed.).Proc. International Conference on Logic Programming and Automated Reasoning (LPAR).LNAI. Vol. 624. Springer. pp. 285–296.Archivedfrom the original on 2016-03-04.Retrieved2014-06-19.Here: Example 3.3
  20. ^Yoshihito Toyama (1987)."Counterexamples to Termination for the Direct Sum of Term Rewriting Systems"(PDF).Inf. Process. Lett.25(3): 141–143.doi:10.1016/0020-0190(87)90122-0.hdl:2433/99946.Archived(PDF)from the original on 2019-11-13.Retrieved2019-11-13.
  21. ^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
  22. ^Wolfram, D. A. (1993).The Clausal Theory of Types.Cambridge University Press. pp. 47–50.doi:10.1017/CBO9780511569906.ISBN9780521395380.S2CID42331173.
  23. ^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.Archivedfrom the original on 2021-08-16.Retrieved2021-08-16.