[Haskell-cafe] Execution order in IO

Albert Y. C. Lai trebla at vex.net
Fri Apr 17 00:35:19 UTC 2015


On 2015-04-15 05:07 AM, Jon Schneider wrote:
> With lazy evaluation where is it written that if you write things with no
> dependencies with a "do" things will be done in order ? Or isn't it ?
>
> Is it a feature of the language we're supposed to accept ?

It is an axiomatic feature. It is unfortunately that the Haskell Report 
is only half-explicit on this. But here it goes.

In Chapter 7, opening:

"The order of evaluation of expressions in Haskell is constrained only 
by data dependencies; an implementation has a great deal of freedom in 
choosing this order. Actions, however, must be ordered in a well-defined 
manner for program execution – and I/O in particular – to be meaningful. 
Haskell’s I/O monad provides the user with a way to specify the 
sequential chaining of actions, and an implementation is obliged to 
preserve this order."

It does not say clearly how you specify an order, but it is going to be 
the >>= operator.

For example,

     main = getLine >>= \_ -> putStrLn "bye"

specifies to stall for your input, and then, to tell you "bye". In that 
order. (Perform an experiment to confirm or refute it!)

* It stalls for your input, even if your input is not needed.

* It tells you "bye", even if you don't need to hear it.

* And it stalls for your input before outputting, not the other way round.

There is no laziness or optimizer re-ordering for this. "An 
implementation is obliged to preserve the order."

In the rest of Chapter 7, several I/O actions from the library are 
described. A few are decribed as "read lazily" --- these are in fact the 
odd men out who postpone inputting, not the common case. The common 
case, where it does not say "read lazily", is to grab input here-and-now 
and produce output here-and-now.

Lastly, throughout the Haskell Report, apart from the few I/O actions 
that "read lazily", there is no other laziness specified. That is, lazy 
evaluation is *not* specified. "An implementation has a great deal of 
freedom in choosing this order."



More information about the Haskell-Cafe mailing list