[Haskell-cafe] Execution order in IO

David Feuer david.feuer at gmail.com
Fri Apr 17 01:06:51 UTC 2015


Others have already discussed this in terms of GHC's model of IO, but as
Tom Ellis indicates, this model is a bit screwy, and not really the best
way to think about it. I think it is much more useful to think of it in
terms of a "free monad". That is, think about the `IO` type as a *data
structure*. An `IO a` value is a sort of recipe for producing a value of
type `a`. That is,

data IO :: * -> * where
  ReturnIO :: a -> IO a
  BindIO :: IO a -> (a -> IO b) -> IO b
  HPutStr :: Handle -> String -> IO ()
  HGetStr :: Handle -> IO String
  ....

And then think about the runtime system as an interpreter whose job is to
run the programs represented by these IO values.
Perhaps I need to be more specific.

main = do
 a <- getLine
 b <- getLine

Can we say "a" absolutely always receives the first line of input and if
so what makes this the case rather than "b" receiving it ? Or do things
need to be slightly more complicated to achieve this ?

Sorry it's just the engineer in me. I think once I've got this clear I'll
be happy to move on.

Jon

_______________________________________________
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/661ed281/attachment.html>


More information about the Haskell-Cafe mailing list