In
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
,
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, and
logic
Logic is the study of correct reasoning. It includes both formal and informal logic. Formal logic is the study of deductively valid inferences or logical truths. It examines how conclusions follow from premises based on the structure o ...
, rewriting covers a wide range of methods of replacing subterms of a
formula
In science, a formula is a concise way of expressing information symbolically, as in a mathematical formula or a ''chemical formula''. The informal use of the term ''formula'' in science refers to the general construct of a relationship betwe ...
with other terms. Such methods may be achieved by rewriting systems (also known as rewrite systems, rewrite engines,
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
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
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 program
A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
s, and several
theorem provers and
declarative programming languages are based on term rewriting.
Example cases
Logic
In
logic
Logic is the study of correct reasoning. It includes both formal and informal logic. Formal logic is the study of deductively valid inferences or logical truths. It examines how conclusions follow from premises based on the structure o ...
, the procedure for obtaining the
conjunctive normal form (CNF) of a formula can be implemented as a rewriting system.
For example, the rules of such a system would be:
:
(
double negation elimination)
:
(
De Morgan's laws)
:
:
(
distributivity
In mathematics, the distributive property of binary operations is a generalization of the distributive law, which asserts that the equality
x \cdot (y + z) = x \cdot y + x \cdot z
is always true in elementary algebra.
For example, in elementary ...
)
:
[This variant of the previous rule is needed since the commutative law ''A''∨''B'' = ''B''∨''A'' cannot be turned into a rewrite rule. A rule like ''A''∨''B'' → ''B''∨''A'' would cause the rewrite system to be nonterminating.]
For each rule, each
variable 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 a
logical equivalence
In logic and mathematics, statements p and q are said to be logically equivalent if they have the same truth value in every model. The logical equivalence of p and q is sometimes expressed as p \equiv q, p :: q, \textsfpq, or p \iff q, depending ...
, 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.
Arithmetic
Term rewriting systems can be employed to compute arithmetic operations on
natural number
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positive in ...
s.
To this end, each such number has to be encoded as a
term.
The simplest encoding is the one used in the
Peano axioms
In mathematical logic, the Peano axioms (, ), also known as the Dedekind–Peano axioms or the Peano postulates, are axioms for the natural numbers presented by the 19th-century Italian mathematician Giuseppe Peano. These axioms have been used nea ...
, based on the constant 0 (zero) and the
successor function
In mathematics, the successor function or successor operation sends a natural number to the next one. The successor function is denoted by ''S'', so ''S''(''n'') = ''n'' +1. For example, ''S''(1) = 2 and ''S''(2) = 3. The successor functio ...
''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.
:
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.
Linguistics
In
linguistics
Linguistics is the scientific study of language. The areas of linguistic analysis are syntax (rules governing the structure of sentences), semantics (meaning), Morphology (linguistics), morphology (structure of words), phonetics (speech sounds ...
,
phrase structure rule
Phrase structure rules are a type of rewrite rule used to describe a given language's syntax and are closely associated with the early stages of transformational grammar, proposed by Noam Chomsky in 1957. They are used to break down a natural langu ...
s, also called rewrite rules, are used in some systems of
generative grammar
Generative grammar is a research tradition in linguistics that aims to explain the cognitive basis of language by formulating and testing explicit models of humans' subconscious grammatical knowledge. Generative linguists, or generativists (), ...
,
as a means of generating the grammatically correct sentences of a language. Such a rule typically takes the form
, where A is a
syntactic category
A syntactic category is a syntactic unit that theories of syntax assume. Word classes, largely corresponding to traditional parts of speech (e.g. noun, verb, preposition, etc.), are syntactic categories. In phrase structure grammars, the ''phrasa ...
label, such as
noun phrase
A noun phrase – or NP or nominal (phrase) – is a phrase that usually has a noun or pronoun as its head, and has the same grammatical functions as a noun. Noun phrases are very common cross-linguistically, and they may be the most frequently ...
or
sentence, and X is a sequence of such labels or
morpheme
A morpheme is any of the smallest meaningful constituents within a linguistic expression and particularly within a word. Many words are themselves standalone morphemes, while other words contain multiple morphemes; in linguistic terminology, this ...
s, 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 a
verb phrase
In linguistics, a verb phrase (VP) is a syntax, syntactic unit composed of a verb and its argument (linguistics), arguments except the subject (grammar), subject of an independent clause or coordinate clause. Thus, in the sentence ''A fat man quic ...
(VP); further rules will specify what sub-constituents a noun phrase and a verb phrase can consist of, and so on.
Abstract rewriting systems
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''
[Book and Otto, p. 10] or ''abstract rewriting system'' (abbreviated ''ARS''). An ARS is simply a set ''A'' of objects, together with a
binary relation
In mathematics, a binary relation associates some elements of one Set (mathematics), set called the ''domain'' with some elements of another set called the ''codomain''. Precisely, a binary relation over sets X and Y is a set of ordered pairs ...
→ on ''A'' called the ''reduction relation'', ''rewrite relation'' or just ''reduction''.
Many notions and notations can be defined in the general setting of an ARS.
is the
reflexive transitive closure of
.
is the
symmetric closure of
.
is the
reflexive transitive symmetric closure of
. The
word problem for an ARS is determining, given ''x'' and ''y'', whether
. An object ''x'' in ''A'' is called ''reducible'' if there exists some other ''y'' in ''A'' such that
; otherwise it is called ''irreducible'' or a
normal form. An object ''y'' is called a "normal form of ''x''" if
, and ''y'' is irreducible. If the normal form of ''x'' is unique, then this is usually denoted with
. If every object has at least one normal form, the ARS is called ''normalizing''.
or ''x'' and ''y'' are said to be ''joinable'' if there exists some ''z'' with the property that
. An ARS is said to possess the
Church–Rosser property if
implies
. An ARS is
confluent if for all ''w'', ''x'', and ''y'' in ''A'',
implies
. An ARS is ''locally confluent'' if and only if for all ''w'', ''x'', and ''y'' in ''A'',
implies
. An ARS is said to be ''terminating'' or ''noetherian'' if there is no infinite chain
. A confluent and terminating ARS is called ''convergent'' or ''canonical''.
Important theorems for abstract rewriting systems are that an ARS is
confluent iff
In logic and related fields such as mathematics and philosophy, "if and only if" (often shortened as "iff") is paraphrased by the biconditional, a logical connective between statements. The biconditional is true in two cases, where either both ...
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
A ''string rewriting system'' (SRS), also known as ''semi-Thue system'', exploits the
free monoid
In abstract algebra, the free monoid on a set is the monoid whose elements are all the finite sequences (or strings) of zero or more elements from that set, with string concatenation as the monoid operation and with the unique sequence of zero ...
structure of the
strings (words) over an
alphabet
An alphabet is a standard set of letter (alphabet), letters written to represent particular sounds in a spoken language. Specifically, letters largely correspond to phonemes as the smallest sound segments that can distinguish one word from a ...
to extend a rewriting relation,
, 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
In mathematics, a tuple is a finite sequence or ''ordered list'' of numbers or, more generally, mathematical objects, which are called the ''elements'' of the tuple. An -tuple is a tuple of elements, where is a non-negative integer. There is o ...
where
is a (usually finite) alphabet, and
is a binary relation between some (fixed) strings in the alphabet, called the set of ''rewrite rules''. The ''one-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
is
symmetric, then the system is called a ''Thue 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 a
congruence, meaning it is an
equivalence relation
In mathematics, an equivalence relation is a binary relation that is reflexive, symmetric, and transitive. The equipollence relation between line segments in geometry is a common example of an equivalence relation. A simpler example is equ ...
(by definition) and it is also compatible with string concatenation. The relation
is called the ''Thue 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 the
presentation of a monoid. Since
is a congruence, we can define the
factor monoid of the free monoid
by the Thue congruence. If a monoid
is
isomorphic
In mathematics, an isomorphism is a structure-preserving mapping or morphism between two structures of the same type that can be reversed by an inverse mapping. Two mathematical structures are isomorphic if an isomorphism exists between the ...
with
, then the semi-Thue system
is called a
monoid presentation of
.
We immediately get some very useful connections with other areas of algebra. For example, the alphabet
with the rules
, where
is the
empty string
In formal language theory, the empty string, or empty word, is the unique String (computer science), string of length zero.
Formal theory
Formally, a string is a finite, ordered sequence of character (symbol), characters such as letters, digits ...
, is a presentation of the
free group
In mathematics, the free group ''F'S'' over a given set ''S'' consists of all words that can be built from members of ''S'', considering two words to be different unless their equality follows from the group axioms (e.g. ''st'' = ''suu''− ...
on one generator. If instead the rules are just
, 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
, 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''.
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
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 a
term algebra
Term may refer to:
Language
*Terminology, context-specific nouns or compound words
**Technical term (or ''term of art''), used by specialists in a field
***Scientific terminology, used by scientists
*Term (argumentation), part of an argument in d ...
. A term can be visualized as a tree of symbols, the set of admitted symbols being fixed by a given
signature
A signature (; from , "to sign") is a depiction of someone's name, nickname, or even a simple "X" or other mark that a person writes on documents as a proof of identity and intent. Signatures are often, but not always, Handwriting, handwritt ...
. As a formalism, term rewriting systems have the full power of
Turing machine
A Turing machine is a mathematical model of computation describing an abstract machine that manipulates symbols on a strip of tape according to a table of rules. Despite the model's simplicity, it is capable of implementing any computer algori ...
s, that is, every
computable function
Computable functions are the basic objects of study in computability theory. Informally, a function is ''computable'' if there is an algorithm that computes the value of the function for every value of its argument. Because of the lack of a precis ...
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
, 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
can be ''applied'' to a term if the left term
matches some
subterm of , that is, if there is some
substitution such that the subterm of
rooted at some
position is the result of applying the substitution
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
with the substitution
applied, see picture 1. In this case,
is said to be ''rewritten in one step'', or ''rewritten 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 be ''rewritten'' to
, formally denoted as
. In other words, the relation
is the
transitive closure
In mathematics, the transitive closure of a homogeneous binary relation on a set (mathematics), set is the smallest Relation (mathematics), relation on that contains and is Transitive relation, transitive. For finite sets, "smallest" can be ...
of the relation
; often, also the notation
is used to denote the
reflexive-transitive closure of
, that is,
if
or A term rewriting given by a set
of rules can be viewed as an abstract rewriting system as defined
above, 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.
[since applying that substitution to the rule's left hand side yields the numerator ] 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
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
In mathematics, the term ''linear'' is used in two distinct senses for two different properties:
* linearity of a '' function'' (or '' mapping'');
* linearity of a '' polynomial''.
An example of a linear function is the function defined by f(x) ...
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,
[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] but not terminating,
[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'')) → ...] and not confluent:
The following two examples of terminating term rewrite systems are due to Toyama:
:
and
:
:
Their union is a non-terminating system, since
This result disproves a conjecture of
Dershowitz, 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
are
linear
In mathematics, the term ''linear'' is used in two distinct senses for two different properties:
* linearity of a '' function'' (or '' mapping'');
* linearity of a '' polynomial''.
An example of a linear function is the function defined by f(x) ...
, and there are no "''overlaps''" between left-hand sides of
and right-hand sides of
. 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
In botany, a tree is a perennial plant with an elongated stem, or trunk, usually supporting branches and leaves. In some usages, the definition of a tree may be narrower, e.g., including only woody plants with secondary growth, only ...
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
*
Critical pair (logic)
*
Compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
*
Knuth–Bendix completion algorithm
*
L-systems specify rewriting that is done in parallel.
*
Referential transparency
In analytic philosophy and computer science, referential transparency and referential opacity are properties of linguistic constructions, and by extension of languages. A linguistic construction is called ''referentially transparent'' when for an ...
in computer science
*
Regulated rewriting
*
Interaction Nets
Notes
Further reading
* 316 pages.
*
Marc Bezem,
Jan Willem Klop,
Roel de Vrijer ("Terese"), ''Term Rewriting Systems'' ("TeReSe"), Cambridge University Press, 2003, . 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 Dershowitz and
Jean-Pierre Jouannaudbr>
"Rewrite Systems" Chapter 6 in
Jan van Leeuwen (Ed.), ''
Handbook of Theoretical Computer Science, Volume B: Formal Models and Semantics.'', Elsevier and MIT Press, 1990, , pp. 243–320. The
preprint
In academic publishing, a preprint is a version of a scholarly or scientific paper that precedes formal peer review and publication in a peer-reviewed scholarly or scientific journal. The preprint may be available, often as a non-typeset versi ...
of this chapter is freely available from the authors, but it is missing the figures.
* Nachum Dershowitz and
David Plaisted"Rewriting" Chapter 9 in
John Alan Robinson and
Andrei Voronkov (Eds.), ''
Handbook of Automated Reasoning, Volume 1''.
*
Gérard Huet et 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 in
Samson Abramsky,
Dov M. Gabbay and
Tom Maibaum (Eds.), ''
Handbook of Logic in Computer Science, Volume 2: Background: Computational Structures''.
* David Plaisted
"Equational reasoning and term rewriting systems" in
Dov M. Gabbay,
C. J. Hogger and
John 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. Book and Friedrich Otto, ''String-Rewriting Systems'', Springer (1993).
* Benjamin Benninghofen, Susanne Kemmerich and
Michael M. Richter, ''Systems of Reductions''.
LNCS 277, Springer-Verlag (1987).
; Other
*
Martin Davis,
Ron Sigal,
Elaine J. Weyuker, (1994) ''Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science – 2nd edition'', Academic Press, .
External links
* Th
Rewriting Home PageIFIP Working Group 1.6Researchers in rewritingby
Aart Middeldorp,
University of Innsbruck
The University of Innsbruck (; ) is a public research university in Innsbruck, the capital of the Austrian federal state of Tyrol (state), Tyrol, founded on October 15, 1669.
It is the largest education facility in the Austrian States of Austria, ...
Termination PortalMaude System— a software implementation of a generic term rewriting system.
References
{{Authority control
Formal languages
Logic in computer science
Mathematical logic
Rewriting systems