In this lecture we can put almost everything together that we have seen so far. Let us look again at one of our examples.
In one of the first lectures, we discussed the high-school algebra problem that was asking whether the equations we know from high-school algebra are enough to do all possible computations allowed in the natural numbers.
In the meanwhile, we have done enough work to be able to unpack the question above. By now we can be precise about all the following.
Exercise: Explain precisely the meaning of "equations", "are enough", "all possible", "computations", "allowed in the natural numbers".
Part of the answer to the exercise above, was computation as rewriting to normal form. So far we took as a model of computation an abstract reduction system (ARS). While each computer, or virtual machine, or program can be understood as an ARS, there are many ARSs that are not models of computation. Therefore, in computer science various refinements of this notion are studied.
In this lecture, we are refining ARSs as follows. The elements of an ARS
are now terms, that is,
To summarise the lecture, we will explain the move from ARSs
to TRSs
As it will turn out, a TRS is completely specified by its equations
The aim of the lecture is to answer precisely, among others, the following questions.
How are the ARSs from the exercises examples of TRSs?
In what sense is computation in a TRS a particular case of reasoning in equational logic?
In what sense are TRSs models of computation?
After the lecture you should test yourself by answering these questions.
To see what is at stake let us look back at the exercises.
In the exercises on string rewriting we defined an ARS
ab -> ba
ba -> ab
aa ->
b ->
Notice that by writing these 4 rewrite rules, we define infinitely many possible reductions
Summary: The reason to go from an ARS
Recall that an equation is a pair
Note that, because an equation is just a pair of terms, every equation can also be understood as a rewrite rule. And vice versa, every rewrite rule should be an equation.
Let us look how this plays out in an example.
Activity 0: In the example
ab -> ba
ba -> ab
aa ->
b ->
what is the signature
An important question in the discussion of ARSs was termination.
Let us see what we have achieved so far by refining ARSs to TRSs.
In most interesting examples of an ARS
On the other hand, we have seen in Activity 0 an infinite set
We will come back to this observation in the Theorem below.
Maybe you have noticed that by going to a TRS, we have turned the meta-variables into object-variables.
In previous examples, for instance in the equations of the lectures on syntax and semantics, we wrote variables
On the other hand, in the term algebra
(In software engineering, sometimes the term reification is used for "turning meta into object". So we could say here that we reified variables.)
This opens up the possibility that we can perform now the operation of pattern matching inside the calculus of equational logic.
In fact, as will see, the rules of substitution and congruence are exactly what is needed for pattern matching.
Let us do a simple computation. (I drop some brackets to make the notation easier to read.)
max(ss0,sss0) -> s(max(s0,ss0))
-> ss(max(0,s0))
-> sss0
Activity 1: Justify each of the steps. [2]
Summary: As an outcome of the activity, we understand now that when we formulate our rewrite-rules/equations with variables and use the rules of equational logic called substitution and congruence, then we can generate infinitely many rewrite-rules from a finite set of equations containing variables using pattern matching.
As a slogan, the rules of substitution and congruence compute pattern matchings.
What we still need to explain better is what we mean by "generating new rewrite-rules from equations using substitution and congruence". But first we should recall again what we mean by "use the rules of equational logic"
As we have seen in the example above, using just the three rewrite-rules/equations
max(sx,sy) -> s(max(x,y))
max(0,x) -> x
max(x,0) -> x
we can compute the max
of aribtrarily large numbers. For example, we can compute max(1024,2048)=1024
, where I write 1024
for a string of 1024 s
followed by a 0
.
As we explained earlier each step in such a computation involves the application of a rule of equational logic. Here are these rules again.
The first three rules say that
and, for all
and a rule for substitution
Also recall
for the set of equations that can be derived from a set of "axioms"
Informally, I understand by pattern matching the following. If we have a structure
It is common to think of the variables in
In our situation, we can be more precise.
The structure will be a term
To decide wether, given
If there are only finitely many equations, then deciding whether a term
Activity 2: Looking back at the derivation
max(ss0,sss0) -> s(max(s0,ss0))
-> ss(max(0,s0))
-> sss0
from the point of view of equational logic, how many steps of equational reasoning does it take to prove that max(ss0,sss0) = sss0
? [3]
The number coming out of the activity is considerably bigger than 3. But we would like to say that we took 3 rewrite steps. The solution is to define from the equations
max(sx,sy) = s(max(x,y))
max(0,x) = x
max(x,0) = x
then ->
to be
So let us define
Definition: Let
if there is one equation
Activity 3: What is the relationship between
We can now summarise what we learned from Activity 1 by saying that if
We will formulate this as a theorem in the next section.
We summarise the work we have done in the examples above by listing again the most important definitions and results. As we only have time to present the bare bones of term rewriting, we recommend Baader-Nipkow to the reader interested in more detail.
Our definition of a TRS is the same as Def.4.2.1 in Baader-Nipkow.
Definition (TRS): Fix a signature and a set of variables. Let
Recall the section substitution.
Definition (matching): We say that a term
Proposition: Matching is decidable.
For more on matching see Section 4.1 in Baader-Nipkow.
The rewrite relation
Definition (
if there is an equation
The next theorem shows that every confluent and terminating TRS is a model of computation. This is Thm 4.1.1 in Baader-Nipkow.
Theorem: If
Proof: We have seen in Activity 3 that
In the first step, we apply the rewrite-rule/equation max(sx,sy) -> s(max(x,y))
. In order to this we need to apply a substitution x:=s0,y:=ss0
, or, written in more mathematical notation, max(ss0,sss0) -> s(max(s0,ss0))
as needed for the first step.
(max(s0,ss0)) -> s(max(0,s0))
, from which, in turn, we derive the rewrite-rule/equation s(max(s0,ss0)) -> ss(max(0,s0))
. The rule of equational logic that justifies this step is called the congruence rule. ↩︎
If I counted correctly, it is 8 applications of rules of equational reasoning: (subst), (subst), (cong), (subst), (cong), (cong), (trans), (trans). ↩︎
The answer is that