In
mathematics,
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, and
logic
Logic is the study of correct reasoning. It includes both formal and informal logic. Formal logic is the science of deductively valid inferences or of logical truths. It is a formal science investigating how conclusions follow from premis ...
, 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,
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 rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
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 execute. Computer programs are one component of software, which also includes documentation and other intangible components.
A computer progra ...
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 science of deductively valid inferences or of logical truths. It is a formal science investigating how conclusions follow from premis ...
, the procedure for obtaining the
conjunctive normal form
In Boolean logic, a formula is in conjunctive normal form (CNF) or clausal normal form if it is a conjunction of one or more clauses, where a clause is a disjunction of literals; otherwise put, it is a product of sums or an AND of ORs. As a cano ...
(CNF) of a formula can be implemented as a rewriting system.
The rules of an example of such a system would be:
:
(
double negation elimination
In propositional logic, double negation is the theorem that states that "If a statement is true, then it is not the case that the statement is not true." This is expressed by saying that a proposition ''A'' is logically equivalent to ''not (no ...
)
:
(
De Morgan's laws
In propositional logic and Boolean algebra, De Morgan's laws, also known as De Morgan's theorem, are a pair of transformation rules that are both valid rules of inference. They are named after Augustus De Morgan, a 19th-century British mathem ...
)
:
:
(
distributivity
In mathematics, the distributive property of binary operations generalizes 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 arithmetic, ...
)
:
[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.]
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 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 number
In mathematics, the natural numbers are those numbers used for counting (as in "there are ''six'' coins on the table") and ordering (as in "this is the ''third'' largest city in the country").
Numbers used for counting are called '' cardinal ...
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 nearly ...
, 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 functi ...
''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 rule numbers are given above the ''rewrites-to'' arrow.
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 human language. It is called a scientific study because it entails a comprehensive, systematic, objective, and precise analysis of all aspects of language, particularly its nature and structure. Lingu ...
,
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 lang ...
s, also called rewrite rules, are used in some systems of
generative grammar
Generative grammar, or generativism , is a linguistic theory that regards linguistics as the study of a hypothesised innate grammatical structure. It is a biological or biologistic modification of earlier structuralist theories of linguistic ...
,
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 ''phrasal ...
label, such as
noun phrase
In linguistics, a noun phrase, or nominal (phrase), is a phrase that has a noun or pronoun as its head or performs the same grammatical function as a noun. Noun phrases are very common cross-linguistically, and they may be the most frequently o ...
or
sentence, and X is a sequence of such labels or
morpheme
A morpheme is the smallest meaningful constituent of a linguistic expression. The field of linguistic study dedicated to morphemes is called morphology.
In English, morphemes are often but not necessarily words. Morphemes that stand alone ar ...
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 syntactic unit composed of a verb and its arguments except the subject of an independent clause or coordinate clause. Thus, in the sentence ''A fat man quickly put the money into the box'', the words ''qu ...
(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 elements of one set, called the ''domain'', with elements of another set, called the ''codomain''. A binary relation over Set (mathematics), sets and is a new set of ordered pairs consisting of ele ...
→ 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
In mathematics, a subset of a given set is closed under an operation of the larger set if performing that operation on members of the subset always produces a member of that subset. For example, the natural numbers are closed under addition, but ...
of
.
is the
symmetric closure In mathematics, the symmetric closure of a binary relation R on a set X is the smallest symmetric relation on X that contains R.
For example, if X is a set of airports and xRy means "there is a direct flight from airport x to airport y", then the ...
of
.
is the
reflexive transitive symmetric closure
In mathematics, a subset of a given set is closed under an operation of the larger set if performing that operation on members of the subset always produces a member of that subset. For example, the natural numbers are closed under addition, but ...
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
In computer science, confluence is a property of rewriting systems, describing which terms in such a system can be rewritten in more than one way, to yield the same result. This article describes the properties in the most abstract setting of an a ...
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" (shortened as "iff") is a biconditional logical connective between statements, where either both statements are true or both are false.
The connective is bicondi ...
it has the Church-Rosser property,
Newman's lemma In mathematics, in the theory of rewriting systems, Newman's lemma, also commonly called the diamond lemma, states that a terminating (or strongly normalizing) abstract rewriting system (ARS), that is, one in which there are no infinite reduction ...
which states that 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 elem ...
structure of the
strings (words) over an
alphabet
An alphabet is a standardized set of basic written graphemes (called letters) that represent the phonemes of certain spoken languages. Not all writing systems represent language in this way; in a syllabary, each character represents a s ...
to extend a rewriting relation,
, to ''all'' strings in the alphabet that contain left- and respectively right-hand sides of some rules as
substring
In formal language theory and computer science, a substring is a contiguous sequence of characters within a string. For instance, "''the best of''" is a substring of "''It was the best of times''". In contrast, "''Itwastimes''" is a subsequence ...
s. Formally a semi-Thue system is a
tuple
In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
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. Obviously
is a subset of
. If the relation
is
symmetric
Symmetry (from grc, συμμετρία "agreement in dimensions, due proportion, arrangement") in everyday language refers to a sense of harmonious and beautiful proportion and balance. In mathematics, "symmetry" has a more precise definit ...
, 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.
Each equivalence relatio ...
(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
In mathematics, a semigroup is an algebraic structure consisting of a set together with an associative internal binary operation on it.
The binary operation of a semigroup is most often denoted multiplicatively: ''x''·''y'', or simply ''xy'', ...
of the free monoid
by the Thue congruence. If a monoid
is
isomorphic with
, then the semi-Thue system
is called a
monoid presentation
In algebra, a presentation of a monoid (or a presentation of a semigroup) is a description of a monoid (or a semigroup) in terms of a set of generators and a set of relations on the free monoid (or the free semigroup ) generated by . The mon ...
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 of length zero.
Formal theory
Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special cas ...
, 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 In mathematics, the bicyclic semigroup is an algebraic object important for the structure theory of semigroups. Although it is in fact a monoid, it is usually referred to as simply a semigroup. It is perhaps most easily understood as the syntactic ...
. 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
In universal algebra and mathematical logic, a term algebra is a freely generated algebraic structure over a given signature. For example, in a signature consisting of a single binary operation, the term algebra over a set ''X'' of variables is exa ...
. A term can be visualized as a tree of symbols, the set of admitted symbols being fixed by a given
signature
A signature (; from la, signare, "to sign") is a Handwriting, handwritten (and often Stylization, stylized) 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 ...
.
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
A match is a tool for starting a fire. Typically, matches are made of small wooden sticks or stiff paper. One end is coated with a material that can be ignited by friction generated by striking the match against a suitable surface. Wooden matc ...
some
subterm of , that is, if there is some
substitution
Substitution may refer to:
Arts and media
*Chord substitution, in music, swapping one chord for a related one within a chord progression
*Substitution (poetry), a variation in poetic scansion
* "Substitution" (song), a 2009 song by Silversun Pic ...
such that the subterm of
rooted at some
position
Position often refers to:
* Position (geometry), the spatial location (rather than orientation) of an entity
* Position, a job or occupation
Position may also refer to:
Games and recreation
* Position (poker), location relative to the dealer
* ...
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 binary relation on a set is the smallest relation on that contains and is transitive. For finite sets, "smallest" can be taken in its usual sense, of having the fewest related pairs; for infinit ...
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
Linearity is the property of a mathematical relationship ('' function'') that can be graphically represented as a straight line. Linearity is closely related to '' proportionality''. Examples in physics include rectilinear motion, the 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,
[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''),''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
Linearity is the property of a mathematical relationship ('' function'') that can be graphically represented as a straight line. Linearity is closely related to '' proportionality''. Examples in physics include rectilinear motion, the linear ...
, 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
In theoretical computer science, in particular in automated reasoning about formal equations, reduction orderings are used to prevent endless loops. Rewrite orders, and, in turn, rewrite relations, are generalizations of this concept that have tu ...
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 term
Lambda calculus (also written as ''λ''-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation th ...
s, 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
Graph may refer to:
Mathematics
*Graph (discrete mathematics), a structure made of vertices and edges
**Graph theory, the study of such graphs and their properties
* Graph (topology), a topological space resembling a graph in the sense of discr ...
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, including only woody plants with secondary growth, plants that are ...
representation.
Trace rewriting systems
Trace theory
In mathematics and computer science, trace theory aims to provide a concrete mathematical underpinning for the study of concurrent computation and process calculi. The underpinning is provided by an algebraic definition of the free partially co ...
provides a means for discussing multiprocessing in more formal terms, such as via the
trace monoid In computer science, a trace is a set of strings, wherein certain letters in the string are allowed to commute, but others are not. It generalizes the concept of a string, by not forcing the letters to always be in a fixed order, but allowing certa ...
and the
history monoid In mathematics and computer science, a history monoid is a way of representing the histories of concurrently running computer processes as a collection of strings, each string representing the individual history of a process. The history monoid pr ...
. Rewriting can be performed in trace systems as well.
Philosophy
Rewriting systems can be seen as programs that infer end-effects from a list of cause-effect relationships. In this way, rewriting systems can be considered to be automated
causality
Causality (also referred to as causation, or cause and effect) is influence by which one event, process, state, or object (''a'' ''cause'') co