Lably
Skip to content

Rewriting

Replacing subterm in a formula with another term

  • 9 min read
  • 5 chapters

Related on Lably

Through Mathematics, Linguistics and Computer science

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 and declarative programming languages are based on term rewriting.

Example cases

Logic

In logic, 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:

¬¬A→A (double negation elimination)
¬(A∧B)→¬A∨¬B (De Morgan's laws)
¬(A∨B)→¬A∧¬B
(A∧B)∨C→(A∨C)∧(B∨C) (distributivity)
A∨(B∧C)→(A∨B)∧(A∨C),

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, 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 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.

A+0→A(1),A+S(B)→S(A+B)(2),A⋅0→0(3),A⋅S(B)→A+(A⋅B)(4).

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)) →(2) S(S(S(0))+S(0)) →(2) S(S(S(S(0))+0)) →(1) S(S(S(S(0)))),

where the notation above each arrow indicates the rule used for each rewrite.

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

S(S(0))⋅S(S(0)) →(4) S(S(0))+S(S(0))⋅S(0) →(4) S(S(0))+S(S(0))+S(S(0))⋅0 →(3) S(S(0))+S(S(0))+0 →(1) S(S(0))+S(S(0)) →s.a. 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, as a means of generating the grammatically correct sentences of a language. Such a rule typically takes the form A→X, 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 S→NPVP 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.

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Computing and communication