"do" notation and ">>"
Koen Claessen
koen@cs.chalmers.se
Wed, 24 Apr 2002 12:15:08 +0200 (MET DST)
I wrote:
| So, changing the translation in GHC might actually introduce
| a very nasty space leak in existing programs!
Simon Peyton-Jones answered:
| It might, conceivably. But the H98 report doesn't
| seem the right place to try to tweak full laziness.
| So I'm going to leave the report as it is. Hugs and
| GHC have changed to match.
I do not understand what full laziness has to do with all
this! The big question is, in the following:
f = do <expr1>
<expr2>
Should <expr2> be shared among different calls to f? It is
clear that <expr1> will, but <expr2> will not be shared,
using the current translation used by GHC and Hugs.
Maybe I should be a bit more concrete; Here is a little
example program:
>>>
main =
do print "start"
writeFile "apa" (show [1..])
<<<
When translating the do-notation using >>, we blow out of
heap space (in both Hugs and GHC (*)). The code then looks
as follows:
>>>
main1 =
print "start" >>
writeFile "apa" (show [1..])
<<<
When translating the do-notation using >>=, we do not blow
out of heap. The code then looks as follows:
>>>
main2 =
print "start" >>= \_ ->
writeFile "apa" (show [1..])
<<<
The reason for this difference is that the computation
"writeFile "apa" (show [1..])" is kept in memory in "main1"
and not in "main2".
So, concretely, the fix in Hugs and GHC will possibly break
a number of programs. Specifically programs that for example
produce a lot of output which does not depend on any
run-time information.
Regards,
/Koen.
(*) Since GHC garbage collects CAFs, we have to add an extra
reference to "main", for example:
main = do print "start"
writeFile "apa" (show [1..])
main
--
Koen Claessen
http://www.cs.chalmers.se/~koen
Chalmers University, Gothenburg, Sweden.