[Haskell-cafe] Remember the future

Andrew Coppin andrewcoppin at btinternet.com
Sat Aug 18 05:07:48 EDT 2007

Dan Piponi wrote:
> On 8/17/07, Dan Piponi <dpiponi at gmail.com> wrote:
>> On 8/17/07, Andrew Coppin <andrewcoppin at btinternet.com> wrote:
>>> That sounds completely absurd to me... can anybody explain?
>> Except...you can switch on ghc's special time travel features...
> On reflection I decided my example isn't very convincing. For one
> thing, I've argued in another thread that monads aren't really about
> sequencing actions. But I concede that there is an exception: the IO
> monad. Because the IO monad has observable side effects you can
> actually see whether or not an action has taken place at a particular
> time, so it really does have to sequence actions. So now consider the
> following code:
>> import IO
>> import Control.Monad.Fix
>> test = mdo
>>     z <- return $ x+y
>>     print "Hello"
>>     x <- readLn
>>     y <- readLn
>>     return z
> Evaluate test and you'll be prompted to enter a pair of numbers.
> You'll then be rewarded with their sum. But the "Hello" message is
> printed before the prompt for input so we know that's being executed
> first. And we can see clearly that the summation is performed before
> the "Hello" message. So clearly this program is computing its result
> before receiving the input.
> At this point your natural reaction should be to replace 'print
> "Hello"' with 'print z'...

Surely all this means is that the magical "mdo" keyword makes the 
compiler arbitrarily reorder the expression...?

More information about the Haskell-Cafe mailing list