Alastair wrote:
| Koen wrote:
|
| > Different people can therefore have different styles of monadic
| > programming:
| >
| > foo 0 = return 5 foo 0 = return 5
| > foo n = foo n =
| > do ... do ...
| > x <- foo (n-1) foo (n-1)
| > return x
| >
| > We know that these two programs do the same.
| >
| > But this is not true operationally!!
|
| It looks to me as though you've redicovered the importance of tail calls.
|
| Whereas tail calls are terrifically important in strict functional
| languages, we lazy programmers have grown used to the idea that
| writing code in a tail-calling style buys you nothing and might even
| cause a space leak.
|
| But monads are often introduce enough constraints over evaluation order that
| bizarre coding styles becomes important again.
It is indead clear that this has to do with tail calls. But what is the
solution? Is it possible to make a writer monad definition that allows
functions like
foo 0 = return ()
foo n =
do write "a"
x <- foo (n-1)
write "b"
return x
to run without space leak? Or are we forced to rewrite "foo"?
Note that the described behavior doesn't hold for _all_ monads. We can of
course take for the definition of W:
type W = IO
write = putStr
And then (at least in Hugs) there is no space leak.
But this is not what you want, because now we are forced to have "foo"
only on toplevel...
Who knows a solution?
Regards,
Koen.
--
Koen Claessen,
[EMAIL PROTECTED],
http://www.cs.chalmers.se/~koen,
Chalmers University of Technology.