[Haskell-cafe] Execution order in IO
haskell at jschneider.net
Thu Apr 16 09:00:31 UTC 2015
I think this is the thing under the bonnet I was after though to be
perfectly honest is slightly beyond me at the time of writing.
Thank you all.
> Let's just have a look at the monad instance of IO which is defined in
> the files ghc-prim/GHC/Types.hs and base/GHC/Base.hs
> newtype IO a = IO (State# RealWorld -> (# State# RealWorld, a #))
> instance Monad IO where
> (>>=) = bindIO
> bindIO :: IO a -> (a -> IO b) -> IO b
> bindIO (IO m) k = IO $ \ s -> case m s of (# new_s, a #) -> unIO (k a)
> If you can forget for a minute about all the # you will end up with this.
> newtype IO a = IO (RealWorld -> (RealWorld, a))
> bindIO (IO m) k = IO $ \ s -> case m s of (new_s, a) -> unIO (k a)
> when the following part is evaluated:
> case m s of (new_s, a) -> unIO (k a)
> (m s) has to be evaluated first in order to ensure that the result
> matches the pattern (new_s, a) and is not bottom/some infinite
> calculation/an error.
> This is why IO statements are evaluated in order.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe