[Haskell-cafe] Execution order in IO
Tom Ellis
tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Thu Apr 16 09:27:47 UTC 2015
Hi Jon,
On Wed, Apr 15, 2015 at 10:07:24AM +0100, 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 ?
I'm not sure where this is written, but it's certainly a property of the IO
type. In the expression
do
x1 <- action1
x2 <- action2
...
then the IO action of the expression `action1` will occur before that of
`action2`.
(As a caveat, one has to be careful about the concept of "when an action
occurs". If `action1` involved reading a file lazily with
`System.IO.readFile`[1], say, then the actual read may not take place until
`action2` has already finished. However, from the point of view behaviour
we consider "observable", a lazy read is indistinguishable from a strict
read. Lazy IO is rather counterintuitive. I suggest you stay away from
it!)
As a side point, appeals to the "real world" in attempts to explain this are
probably unhelpful at best. GHC may well implement IO using a fake value of
type `RealWorld` but that's beside the point. A conforming Haskell
implementation is free to implement IO however it sees fit.
> Is it a feature of the language we're supposed to accept ?
Sort of. It's a property of the IO type.
> Is it something in the implementation of IO ?
Yes.
> Is the do keyword more than just a syntactic sugar for a string of binds
> and lambdas ?
No.
Tom
[1] http://hackage.haskell.org/package/base-4.8.0.0/docs/System-IO.html#v:readFile
More information about the Haskell-Cafe
mailing list