Is this a reasonable view of Haskell IO?
When given a program, the Haskell runtime does the following:
- Calls
mainto get back an “IO computation” - It then executes or “runs” that computation thereby performing all of the side-effects the computation contains.
This two-stage approach allows main to remain a pure function.
An IO computation in this case is like a special version of Haskell that has explicit sequencing – or perhaps there is a better way to describe this?
Yeah, that’s a decent semantic model of how the programs are executed. The implementation doesn’t work like that, of course, but you can still use that model to reason about the programs.
But more generally, what
IOdoes is to allow you to treat imperative programs as pure values. TheMonadoperations then allow you to compose imperative programs from smaller imperative programs (or to use the usual term in this context, actions) and pure functions. So the purely functional model, while it cannot execute imperative programs, can still describe them as expressions of typeIO a, and the compiler can translate these descriptions into imperative code.Or you could say this:
main.I.e., the “evaluate
main” part of your model is pushed to the compiler, and is not in the runtime as you first describe it.