To do the exercises you need to use the material about relations and syntax and semantics and abstract reduction systems and termination.
The first exercise is meant to be easy. If it is not, and assuming that you revised the material linked above, the reason must be that I didn't explain some background you need … let me know if this is case.
In this exercise we rewrite strings over letters a,b
and write w -> w'
for a pattern or schema of rules that allows to reduce strings by replacing any occurrence of w
by w'
. The rule schema w ->
with empty right-hand side allows us to erase w
from any word in which w
occurs.
Consider the schemas of rules
ab -> ba
ba -> ab
aa ->
b ->
abba
and bababa
.Consider the schemas of rules
ba -> bbaa
aa ->
ba -> ab
ab -> ba
ab
to aabb
?ba
to abbaababbab
?ba
to abbaababbaba
?ba
?The purpose of the next exercise is for you to apply the new technical notions to an example you know well already.
In the exercise, the bijective correspondence of equivalence classes with positive fractions sets up a semantics as discussed here. The point of the exercise was to work from the semantics towards a syntax that captures it.
In the next exercise, we work in the other direction. I give you an ARS that will look unfamiliar to you. Can you find an interpretation, or maybe just an invariant (see the footnotes) and use it to show that the string OK
cannot be reached from OR
?
If the last letter is R you may add a K at the end
A string of the form Ox may be rewritten to Oxx
You may replace any occurrence of RRR by K
You may erase any occurrence of KK
Above, x
is a variable that maybe replaced by any string.
OK
to OR
?OR
to OK
?As it often happens with this kind of exercises, it can be quite tricky until you suddenly see the solution. But trying to understand what is going on pays off in any case.
The next exercise illustrates that there is a tight connection between reductions in ARSs and the evaluation of recursive functions. In fact, ARSs are the model of computation behind recursive functions.
The A-function has a recursive definition as follows.
a(0,n) = n+1
a(m+1,0) = a(m,1)
a(m+1,n+1) = a(m, a(m+1,n))
The next example is typical for a situation where the elements of the ARS are not mere strings but terms. We encountered this before when we discussed arithmetic expressions. Recall how terms really are trees, even if written in linear (or, as we sometimes say, one-dimensional) notation.
Consider the ARS given by [^Dershowitz]
max(0,x) -> x
max(x,0) -> x
max(s(x),s(y)) -> s(max(x,y))
min(0,x) -> 0
min(x,0) -> 0
min(s(x),s(y)) -> s(min(x,y))
sort([]) -> []
sort([x | xs]) -> insert(x,xs)
insert(x,[]) -> [x]
insert(x,[y|ys]) -> [min(x,y)|insert(max(x,y),ys)]
where
[]
and 0
s
and sort
[-|-]
and min
and max
and insert
x
and y
and xs
and ys
Do the following exercises.
min
, max
, sort
, insert
.min
, max
, sort
, insert
as functions? Your answer should make use of what we learned about syntax and semantics.Review the Basic Examples above and describe them in terms of TRSs. What are the signature, variables and equations? [4]
Choose a simple algorithm and formulate it as a rewriting system as in the exercise on sorting above. Write a blog post about it. Add in as much as you want and can of the material we learned so far, including the lectures on TRSs.
(The termination exercises are taken from Baader-Nipkow.)
Exercise: Show that whatever the test <TEST>
the program below
while ub > lb + 1 do
begin r : = (ub + lb) div 2;
if <TEST> then ub := r else lb := r
end
terminates. Are there any assumptions you need do make the argument work?
Exercise: Show that the two programs
while m =/= n do
if m > n then m := m — n else n := n — m
and
while m =/= n do
if m > n then m : = m — n
else begin h :=m; m :=n; n := h end
terminate. Are there any assumptions you need do make the argument work?
Write a program in your programming language that contains a while loop or recursive calls and show termination by exhibiting a measure function. Write a blog post about it.
Exercise: What do the following two programs compute? What pre and postconditions can be used to formalise this? Find a loop invariant and use it to prove the partial correctness of this program.
while (i < 100 ) do
y := y+x
i := i+1
done
while (i < k ) do
i := i+1
y := y*i
done
Go back to your on termination of a while loop. In your blog, discuss the partial correctness from the point of the loop. (Or, alternatively, choose another program with a while loop.)
Describe the data-types of lists and sets by operations and equations. For the operations you may have a look at lists in python and sets in python but choose a small set of operations that seem essential.
Reduce the following lambda terms
Reduce
You may use the following equations:
Are all reduction sequences starting from
Decide whether the folloing lambda terms are typable. If they are typable, derive the most general type. Justify your answer.
The exercises in this section should be fun or be intersting for various reasons, but if you have done the ones above you should be fine.
Show that the following process always terminates. There is a box full
of black and white balls. Each step consists of removing an arbitrary
ball from the box. If it happens to be a black ball, one also adds an
arbitrary (but finite) number of white balls to the box.
Go back to your class on data structures and algorithms
and find an algorithm based on a while-loop and analyse it
from the point of view of invariants and partial correctness.
Suggest a rule to add in Hoare Logic for the statement
repeat S until B
The repeat statement first executes the statement S
and then checks for the condition B.
Let us look at the function
that maps a good to its price. For our purposes, we can identify
A function
Assuming that you encode the fraction
An ARS
STOP reading if you do not want to see hints at the solution.
In case of string rewriting, the question is what are the operations that we use to form words such as aba
from letters a
and b
? There are at least three possibilities.
In case of the sorting example, the signature is given by constants 0
, []
, unary operation symbols s
, sort
, and binary operation symbols [-|-]
, min
, max
, and insert
. We can also refine this to a "many-sorted signature" by introducing types nat
and natlist
and say that operation symbols are typed as follows (in, hopefully, self-explanatory notation)
0 : nat
s : nat -> nat
max : nat,nat -> nat
min : nat,nat -> nat
[] : list
[-|-] : nat, natlist -> natlist
insert : nat, natlist -> nat
sort : natlist -> natlist
We take lambda-terms here up to