[Haskell-cafe] ODP: Execution order in IO

Marcin Mrotek marcin.jan.mrotek at gmail.com
Thu Apr 16 10:31:52 UTC 2015


Just for the record, lazy IO uses black magic called unsafeInterleaveIO under the hood, which, as the name suggests, deliberately interferes with the execution order imposed by binds.

Best regards,
Marcin Mrotek 

-----Wiadomość oryginalna-----
Od: "Tom Ellis" <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk>
Wysłano: ‎2015-‎04-‎16 11:28
Do: "haskell-cafe at haskell.org" <haskell-cafe at haskell.org>
Temat: Re: [Haskell-cafe] Execution order in IO

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
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150416/18bbf7b7/attachment.html>


More information about the Haskell-Cafe mailing list