This code (taken from Learn You A Haskell):
main = do putStr "Hey, "
putStr "I'm "
putStrLn "Andy!"
apparently desugars to
main = putStr "Hey, " >>=
(\_ -> putStr "I'm " >>=
(\_ -> putStrLn "Andy!"))
Which, as I understand it can be interpretted as saying "In order to putStrLn "Andy!" I first need to putStr "I'm ", and in order to do that I first need to putStr "Hey, ";
I disagree with this interpretation, which is annoying because the compiler obviously doesn't and leaves me feeling confused. The problem I have with it is that the lambdas ignore their arguments, during lazy evaluation isn't this sort of thing supposed to be recognised and short-circuited?
Also, sure, the binding returns an IO action, and when that IO action falls into main it gets executed. But what's to stop it from printing "Hey, Andy!I'm "? I suspect it's whatever bind is doing.
Also, how does an IO action of type "IO ()" carry enough information to allow the runtime system to print "Hey, I'm Andy!"? How is that IO () different to an IO () than prints "Hello World!" or writes to a file?
Consider another, from the wikipedia page for monad:
Sugared version:
do
putStrLn "What is your name?"
name <- getLine
putStrLn ("Nice to meet you, " ++ name ++ "!")
Desugared version:
putStrLn "What is your name?" >>=
(\_ ->
getLine >>=
(\name ->
putStrLn ("Nice to meet you, " ++ name ++ "!")))
Similar story here.
I think I just need to see the definition of bind for IO and then it will be all clear. Something else that would help a lot is if someone could help me step through how the program actually gets evaluated and identify the exact moments when the side effects occur.
(>>=)
is a particularly lazy function here, but the(>>=)
function itself is not lazy. – Pinnatiped