<p>I don't think it's correct to call them synonyms; there is some overlap in terms of research and implementation. I'm not familiar at all with Jay's work, and I'm only somewhat familiar with term rewriting systems, so I may be missing something too.</p>

<p>Pattern matching in general deals with the following problem: you have some structure (a tree or a list or a multiset) and you want to check whether the structure matches a pattern (or one of a number of patterns). This question is certainly relevant to term rewriting, because in term rewriting systems the fact that a term matches a pattern means that the term can be rewritten to a different term, but it's not synonymous term rewriting. (There may be a formulation of pattern matching as rewriting: "Given a term, can you rewrite it to match pattern?" but I've never seen this.) </p>

<p>Pattern matching in a functional programming language has a logical interpretation in terms of focusing (see <a href="http://portal.acm.org/citation.cfm?id=1480927">Krishnaswami's "Focusing on Pattern Matching"</a>, for example). Term rewriting systems, on the other hand, often do matching modulo some equational properties, which isn't present in most functional programming languages (you can't match against a multiset in ML or Haskell). There's no fundamental reason why matching modulo equational properties shouldn't be present in functional languages, however. </p>

CC BY-SA 3.0
2011-04-26T17:45:57.403Z
Thanks for your answer. I agree that *pattern matching* in general is not synonymous with *term rewriting* but it is more basical. But if someone says that a systems with computational power is based on pattern matching, I cannot see the difference to a term rewriting system with computational power. Can you further exemplify the difference between "has a logical interpretation" and "some equational properties"?
Jakob
CC BY-SA 3.0
2011-04-27T12:25:43.910Z
"I cannot see the difference to a term rewriting system with computational power" - I'm not sure what this means. As Martin says, term rewriting is a general model of computation, and pattern matching is a feature, not a model of computation.
Rob Simmons
CC BY-SA 3.0
2011-04-27T12:27:07.230Z
_Can you further exemplify the difference between "has a logical interpretation" and "some equational properties"?_ - There's not any sort of shallow difference - they're just different properties, apples and oranges. I think that any actual connection between these two might turn out to be a pretty deep research question! Pun with deep inference - see http://alessio.guglielmi.name/res/cos/ - probably intended.
Rob Simmons
CC BY-SA 3.0
2011-04-26T12:30:51.303Z
2011-04-26T12:30:51.303Z
Rob Simmons
<p>(I'd rather write this as a comment, but I can't at present.)</p>

<p>Correct me if I'm wrong, but as far as I understand, one more difference between pattern matching and term rewriting, apart from <a href="https://cstheory.stackexchange.com/a/6266/10146">what Martin Berger said in his excellent answer</a>, is that pattern matching rules come with a <em>fixed order</em> (in implementations like Haskell's), whereas with term rewriting rules this is not necessarily the case. This feature, as one would expect, can make a lot of difference when considering the behavior (in particular, termination) of the rules (see <a href="https://www.haskell.org/tutorial/patterns.html" rel="nofollow noreferrer">"A Gentle Introduction to Haskell, Version 98", section 4.2</a>, for example, or just the factorial example at <a href="http://learnyouahaskell.com/syntax-in-functions" rel="nofollow noreferrer">"Learn you a Haskell"</a>).</p>

<p>People more knowledgeable in rewriting theory would have more to say on that (for example, how does typing fit such a comparison exactly?), but, it seems to me fair to agree with Martin Berger, in that <em>term rewriting can be seen to encompass pattern matching</em> (at least as this is implemented in languages like Haskell), to the extent that both can be (rather dryly) viewed as devices which merely employ term-relating rules.</p>

CC BY-SA 3.0
2014-11-04T16:21:23.713Z
2014-11-04T16:32:01.903Z
2017-04-13T12:32:30.623Z
Basil
<p>One way of looking at these two concepts is to say that pattern matching is <i>a feature of programming languages</i> for combining discrimination on constructors and destructing terms (while at the same time selecting and locally naming term fragments) safely, compactly and efficiently. Research on pattern matching typically focusses on implementation efficiency, e.g. on how to minimise the number of comparisons the matching mechanism has to do. </p>

<p>In contrast, term rewriting is a general <i>model of computation</i> that investigates a wide range of (potentially non-deterministic) methods of replacing subterms of syntactic expressions (more precisely an element of a term-algebra over some set of variables) with other terms. Research on term rewriting systems is usually about abstract properties of rewriting systems such as confluence, determinism and termination, and more specifically about how such properties are or are not preserved by algebraic operations on rewrite systems, i.e. to what extent these properties are compositional.</p>

<p>Clearly there are conceptual overlaps between both, and the distinction is to a degree traditional, rather than technical. A technical difference is that term rewriting happens under arbitrary contexts (i.e. a rule $(l, r)$ induces rewrites $C[l\sigma] \rightarrow C[r\sigma]$ for arbitrary contexts $C[.]$ and substitutions $\sigma$), while pattern matching in modern languages like Haskell, OCaml or Scala provides only for rewriting 'at the top' of a term. This restriction is also, I think, imposed in Jay's pattern calculus.
Let me explain what I mean by this restriction. With pattern matching in the OCaml, Haskell, Scala sense, you cannot say something like</p>

<pre><code>match M with
   | C[ x :: _ ]  -&gt; printf "%i ...\n" x
   | C[ [] ] -&gt; printf "[]"
</code></pre>

<p>What is <code>C[.]</code> here? It's supposed to be a
variable that ranges over one-holed contexts.  But languages like
OCaml, Haskell or Scala don't give programmers variables that
range over arbitrary (one-holed) contexts, only variables that range
over values.  In other words, in such languages you cannot pattern
match at an arbitrary position in a term. You always have to specify
the path from the root of the pattern to the parts that you are
interested in.  I guess the key reason for imposing this restriction
is that otherwise pattern matching would be non-deterministic, because
a term might match a pattern in more than one way. For example the
term <code>(true, [9,7,4], "hello", 7)</code> matches the
pattern <code>C[7]</code> in two ways, assuming <code>C[.]</code>
ranged over such contexts.</p>

CC BY-SA 3.0
2011-04-26T20:28:30.137Z
2013-12-27T14:43:31.250Z
2013-12-27T14:43:31.250Z
Martin Berger
<p>As there was <a href="http://lambda-the-ultimate.org/node/4232">no response at Lambda the Ultimate</a> I try it here again: <em>term rewriting systems</em> are used for instance in automated theorem proving a symbolic calculation, and of course to define formal grammars. There are some programming languages based in term rewriting, but as far as I understand the concept is more known as <em>pattern matching</em>. Pattern matching is used a lot in 
functional languages. <a href="http://lambda-the-ultimate.org/node/3695">Barry Jay has created</a> a whole theory called <em>pattern calculus</em>, but he only mentions term rewriting in brief. I have the feeling that they all refer to the same basic idea, so can you use <em>term rewriting</em> and <em>pattern matching</em> synonymously?</p>

<pl.programming-languages><terminology><term-rewriting-systems>
What's the difference between term rewriting and pattern matching?
Jakob
# What's the difference between term rewriting and pattern matching?

(Asked by: username_0 on 2011-04-26)

As there was [no response at Lambda the Ultimate](http://lambda-the-ultimate.org/node/4232) I try it here again: *term rewriting systems* are used for instance in automated theorem proving a symbolic calculation, and of course to define formal grammars. There are some programming languages based in term rewriting, but as far as I understand the concept is more known as *pattern matching*. Pattern matching is used a lot in
functional languages. [Barry Jay has created](http://lambda-the-ultimate.org/node/3695) a whole theory called *pattern calculus*, but he only mentions term rewriting in brief. I have the feeling that they all refer to the same basic idea, so can you use *term rewriting* and *pattern matching* synonymously?

**Tags:** pl.programming-languages, terminology, term-rewriting-systems

---

## 3 answers

### Answer by username_3 on 2011-04-26. Score: 27 | Accepted answer

One way of looking at these two concepts is to say that pattern matching is a feature of programming languages for combining discrimination on constructors and destructing terms (while at the same time selecting and locally naming term fragments) safely, compactly and efficiently. Research on pattern matching typically focusses on implementation efficiency, e.g. on how to minimise the number of comparisons the matching mechanism has to do. 
In contrast, term rewriting is a general model of computation that investigates a wide range of (potentially non-deterministic) methods of replacing subterms of syntactic expressions (more precisely an element of a term-algebra over some set of variables) with other terms. Research on term rewriting systems is usually about abstract properties of rewriting systems such as confluence, determinism and termination, and more specifically about how such properties are or are not preserved by algebraic operations on rewrite systems, i.e. to what extent these properties are compositional.
Clearly there are conceptual overlaps between both, and the distinction is to a degree traditional, rather than technical. A technical difference is that term rewriting happens under arbitrary contexts (i.e. a rule $(l, r)$ induces rewrites $C[l\sigma] \rightarrow C[r\sigma]$ for arbitrary contexts $C[.]$ and substitutions $\sigma$), while pattern matching in modern languages like Haskell, OCaml or Scala provides only for rewriting 'at the top' of a term. This restriction is also, I think, imposed in Jay's pattern calculus.
Let me explain what I mean by this restriction. With pattern matching in the OCaml, Haskell, Scala sense, you cannot say something like
match M with
   | C[ x :: _ ]  -> printf "%i ...\n" x
   | C[ [] ] -> printf "[]"

What is C[.] here? It's supposed to be a
variable that ranges over one-holed contexts.  But languages like
OCaml, Haskell or Scala don't give programmers variables that
range over arbitrary (one-holed) contexts, only variables that range
over values.  In other words, in such languages you cannot pattern
match at an arbitrary position in a term. You always have to specify
the path from the root of the pattern to the parts that you are
interested in.  I guess the key reason for imposing this restriction
is that otherwise pattern matching would be non-deterministic, because
a term might match a pattern in more than one way. For example the
term (true, [9,7,4], "hello", 7) matches the
pattern C[7] in two ways, assuming C[.]
ranged over such contexts.


---

### Answer by username_1 on 2011-04-26. Score: 11

I don't think it's correct to call them synonyms; there is some overlap in terms of research and implementation. I'm not familiar at all with Jay's work, and I'm only somewhat familiar with term rewriting systems, so I may be missing something too.

Pattern matching in general deals with the following problem: you have some structure (a tree or a list or a multiset) and you want to check whether the structure matches a pattern (or one of a number of patterns). This question is certainly relevant to term rewriting, because in term rewriting systems the fact that a term matches a pattern means that the term can be rewritten to a different term, but it's not synonymous term rewriting. (There may be a formulation of pattern matching as rewriting: "Given a term, can you rewrite it to match pattern?" but I've never seen this.)

Pattern matching in a functional programming language has a logical interpretation in terms of focusing (see [Krishnaswami's "Focusing on Pattern Matching"](http://portal.acm.org/citation.cfm?id=1480927), for example). Term rewriting systems, on the other hand, often do matching modulo some equational properties, which isn't present in most functional programming languages (you can't match against a multiset in ML or Haskell). There's no fundamental reason why matching modulo equational properties shouldn't be present in functional languages, however.

> **Comments:**
> **username_0**: Thanks for your answer. I agree that *pattern matching* in general is not synonymous with *term rewriting* but it is more basical. But if someone says that a systems with computational power is based on pattern matching, I cannot see the difference to a term rewriting system with computational power. Can you further exemplify the difference between "has a logical interpretation" and "some equational properties"?  
> **username_1**: "I cannot see the difference to a term rewriting system with computational power" - I'm not sure what this means. As Martin says, term rewriting is a general model of computation, and pattern matching is a feature, not a model of computation.  
> **username_1**: _Can you further exemplify the difference between "has a logical interpretation" and "some equational properties"?_ - There's not any sort of shallow difference - they're just different properties, apples and oranges. I think that any actual connection between these two might turn out to be a pretty deep research question! Pun with deep inference - see http://alessio.guglielmi.name/res/cos/ - probably intended.  

---

### Answer by username_2 on 2014-11-04. Score: 1

(I'd rather write this as a comment, but I can't at present.)

Correct me if I'm wrong, but as far as I understand, one more difference between pattern matching and term rewriting, apart from [what Martin Berger said in his excellent answer](https://cstheory.stackexchange.com/a/6266/10146), is that pattern matching rules come with a *fixed order* (in implementations like Haskell's), whereas with term rewriting rules this is not necessarily the case. This feature, as one would expect, can make a lot of difference when considering the behavior (in particular, termination) of the rules (see ["A Gentle Introduction to Haskell, Version 98", section 4.2](https://www.haskell.org/tutorial/patterns.html), for example, or just the factorial example at ["Learn you a Haskell"](http://learnyouahaskell.com/syntax-in-functions)).

People more knowledgeable in rewriting theory would have more to say on that (for example, how does typing fit such a comparison exactly?), but, it seems to me fair to agree with Martin Berger, in that *term rewriting can be seen to encompass pattern matching* (at least as this is implemented in languages like Haskell), to the extent that both can be (rather dryly) viewed as devices which merely employ term-relating rules.


---

