[Haskell-cafe] Re: evaluation semantics of bind

Jonathan Cast jonathanccast at fastmail.fm
Thu Feb 5 19:16:09 EST 2009

On Fri, 2009-02-06 at 00:51 +0100, Peter Verswyvelen wrote:
> On Thu, Feb 5, 2009 at 8:20 PM, ChrisK <haskell at list.mightyreason.com>
> wrote:
>         Since this is strict there is no laziness and the code must
>         evaluate the input and output "State RealWorld" to ensure they
>         are not bottom or error.

> Interesting. I also thought it was the passing of the RealWorld that
> caused the sequencing, I never realized that the strictness played an
> important role here. 

> So what would happen if it would be lazy instead of strict? What kind
> of craziness would occur?

The order of side effects would be demand-driven, rather than
order-of-statement driven.  So if I said:

     x <- getChar
     y <- getChar
     z <- getChar

then in the sequel, the first character I evaluated would be the first
character read.  Of course, Haskell's order of evaluation is undefined,
so whether the string read from STDIN was

   [x, y, z]
or [y, x, z]
or [z, x, y]

or some other permutation would be undefined.  And, of course, if I
never evaluated y at all, y would never be read --- there would be only
two characters of input.

Essentially, the program would act as if every statement was wrapped up
in an unsafeInterleaveIO.


More information about the Haskell-Cafe mailing list