4.15 Exercises 149
This discussion should have indicated that when designing garbage collection
algorithms for virtual machines like the W
IM, the assumptions made by the trans-
lation schemes must be taken into account. The garbage collector for the W
IMis
not as efficient as the garbage collector for the M
AMA: one of the reasons is that it
is no longer possible to visit live heap objects only. Instead, the whole old memory
segment is traversed (including the non-reachable heap objects) during the copying
phase to ensure the correct order of objects after copying.
4.15 Exercises
1. Lists in PRO LO G. Implement the following predicates in PROLOG :
• last
/2, where the first parameter is a list and the second is the last element
of this list. For instance, the following fact should be provable:
last
([1, 2, 3],3)
• reverse/2 with two argument lists in reverse element order. For instance,
the following fact should be provable:
reverse
([1, 2, 3, 4], [4, 3, 2, 1])
• chain/2 with two lists where the second is contained in the first as a con-
secutive partial list. For instance:
chain
([1, 2, 3, 4, 5, 6], [2, 3, 4])
• remove/3 with one value and two lists as parameters. The second list is
supposed to be identical to the first except for the removalof all occurrences
of the first parameter. For instance:
remove
(2, [1, 2, 3, 2, 5], [1, 3, 5])
Hint: You may introduce auxiliary predicates.
2. Programming in P
RO L. Consider the following PRO L program:
edge
(X, Y) ⇐ X = a, Y = b
edge
(X, Y) ⇐ X = b, Y = a
edge
(X, Y) ⇐ X = c, Y = c
reachable
(X, Y) ⇐ X = Y
reachable
(X, Y) ⇐ edge(X, Z), reachable(Z, Y)
⇐
reachable(a, c)
How does this program behave when executed? Explain its behavior.
3. Translation of Terms and Goals. Generate code
A
and code
G
for the follow-
ing terms/goals.