This preview shows pages 1–3. Sign up to view the full content.
348
Declarative Concurrency
fun
lazy {Three} {Delay 1000} 3
end
Calculating
{Three}+0
returns 3 after a 1000 millisecond delay. This is as
expected, since the addition needs the result of
{Three}
.N
owc
a
l
c
u
l
a
t
e
{Three}+0
three times in succession.
Each
calculation waits 1000 millisec
onds. How can this be, since
Three
is supposed to be lazy. Shouldn’t its
result be calculated only once?
11.
Laziness and concurrency I
. This exercise looks closer at the concurrent
behavior of lazy execution. Execute the following:
fun
lazy {MakeX} {Browse x} {Delay 3000} 1
end
fun
lazy {MakeY} {Browse y} {Delay 6000} 2
end
fun
lazy {MakeZ} {Browse z} {Delay 9000} 3
end
X={MakeX}
Y={MakeY}
Z={MakeZ}
{Browse (X+Y)+Z}
This displays
x
and
y
immediately,
z
after 6 seconds, and the result 6 after
15 seconds. Explain this behavior. What happens if
(X+Y)+Z
is replaced by
X+(Y+Z)
or by
thread
X+Y
end
+Z
? Which form gives the ﬁnal result
the quickest? How would you program the addition of
n
integers
i
1
, ...,
i
n
, given that integer
i
j
only appears after
t
j
milliseconds, so that the ﬁnal
result appears the quickest?
12.
Laziness and concurrency II
. Let us compare the kind of incremental
ity we get from laziness and from concurrency. Section 4.3.1 gives a pro
ducer/consumer example using concurrency. Section 4.5.3 gives the same
producer/consumer example using laziness. In both cases, it is possible for
the output stream to appear incrementally. What is the diﬀerence? What
happens if you use both concurrency and laziness in the producer/consumer
example?
13.
Laziness and monolithic functions
. Consider the following two deﬁni
tions of lazy list reversal:
fun
lazy {Reverse1 S}
fun
{Rev S R}
case
S
of
nil
then
R
[] XS2
then
{Rev S2 XR}
end
end
in
{Rev S nil}
end
fun
lazy {Reverse2 S}
fun
lazy {Rev S R}
Copyright
c
±
20013 by P. Van Roy and S. Haridi. All rights reserved.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 4.11 Exercises
349
case
S
of
nil
then
R
[] XS2
then
{Rev S2 XR}
end
end
in
{Rev S nil}
end
What is the diﬀerence in behavior between
{Reverse1 [a b c]}
and
{Reverse2 [a b c]}
? Do the two deﬁnitions calculate the same result?
Do they have the same lazy behavior? Explain your answer in each case.
Finally, compare the execution eﬃciency of the two deﬁnitions.
Which
deﬁnition would you use in a lazy program?
14.
Laziness and iterative computation
.
In the declarative model, one
advantage of dataﬂow variables is that the straightforward deﬁnition of
Append
is iterative.
For this exercise, consider the straightforward lazy
version of
Append
without dataﬂow variables, as deﬁned in Section 4.5.7.
Is it iterative? Why or why not?
15.
Performance of laziness
. For this exercise, take some declarative pro
grams you have written and make them lazy by declaring all routines as
lazy. Use lazy versions of all builtin operations, for example addition be
comes
Add
, which is deﬁned as
fun
lazy {Add X Y} X+Y
end
.Compa
re
the behavior of the original eager programs with the new lazy ones. What is
the diﬀerence in eﬃciency? Some functional languages, such as Haskell and
Miranda, implicitly consider all functions as lazy. To achieve reasonable
This is the end of the preview. Sign up
to
access the rest of the document.
This document was uploaded on 08/10/2011.
 Spring '11

Click to edit the document details