Rodney D Price wrote:
Okay... However, when I use IO in a Haskell program, the response is usually pretty snappy. It's not as if the Haskell runtime is hanging around, waiting for some time in the future when it might be appropriate to do IO. It happens right now. Yet the literature gives the impression that the IO monad in particular is a clever trick to preserve referential transparency by gathering up all the IO actions, but not necessarily actually *performing* them. Then the Haskell runtime holds its nose and *performs* them when necessary.
It's really best to think of 'main' as returning an IO action. You can call >>= all over the place, but if you never return the result up through 'main', the resulting bound action will never be executed. (Even then it might not be executed, if the runtime never passes through that point in your program.) Coming from an imperative background, I imagine that 'main' returns pretty much immediately, and the runtime starts executing your action right away. To put it another way, 'main' is a constant -- it doesn't do work, so much as it describes what you want the runtime to do. Hope this helps!