block, you can just imagine a chain of applications of <code>(>>)</code> and <code>(>>=)</code>,

block, you can just imagine a chain of applications of <code>(>>)</code> and <code>(>>=)</code>,

−

and some lambdas when appropriate to capture the results of actions. An action on its own on a line will be executed, and a line of the form

+

and some lambdas when appropriate to capture the results of actions. An action on its own on a line in a do-block will be executed, and a line of the form <code>v <- x</code> will cause the action <code>x</code> to be run, and the result bound to the variable <code>v</code>.

−

v <- x

+

−

will cause the action <code>x</code> to be run, and the result bound to the variable v.

+

Note that there is no function:

Note that there is no function:

Revision as of 09:06, 9 May 2006

(This page is intended as a quick introduction to how IO is treated in Haskell. It doesn't describe everything there is to know, but should give some idea of how things work.)

Haskell separates pure functions from computations where side effects
must be considered by encoding those side effects as values of a
particular type. Specifically, a value of type (IO a) is an action,
which if executed would produce a value of type a.

Some examples:

getLine::IOStringputStrLn::String->IO()-- note that the result value is an empty tuple.
randomRIO ::(Random a)=>(a,a)->IO a

Ordinary Haskell evaluation doesn't cause this execution to occur. A
value of type (IO a) is almost completely inert. In fact, the only IO
action which can really be said to run in a compiled Haskell program
is main.

Armed with this knowledge, we can write a "hello, world" program:

main ::IO()
main =putStrLn"Hello, World!"

Now, so far, all this is great, but without a way to chain actions
together end-to-end, we can't do a whole lot. So Haskell provides us
with a few primitives for composing and chaining together IO actions.
A simple one of these is:

(>>)::IO a ->IO b ->IO b

where if x and y are IO actions, then (x >> y) is the action that
performs x, dropping the result, then performs y and returns its
result.
Great, we can now write programs which do multiple things:

main =putStrLn"Hello">>putStrLn"World"

will print "Hello" and "World" on separate lines.

However, we don't yet have a way to chain actions in which we are
allowed to use the result of the first in order to affect what the
second action will do. This is accomplished by the following
operation, called 'bind':

(>>=)::IO a ->(a ->IO b)->IO b

Now, x >>= f is the action that first performs the action x, and captures its result, passing it to f, which then computes a second action to be performed. That action is then carried out, and its result is the result of the overall computation.

That's a mouthful, but once you see it in use, perhaps the idea will
become clearer:

main =putStrLn"Hello, what is your name?">>getLine>>= \name ->putStrLn("Hello, "++ name ++"!")

This is most of what we need. In fact, this bind function is really
successful, and we can define (>>) in terms of it:

x >> y = x >>=const y

In practice, it turns out to also be quite important to turn a value
into an IO action which does nothing, and simply returns that value.
This is quite handy near the end of a chain of actions, where we want
to decide what to return ourselves, rather than leaving it up to the
last action in the chain. So there's one more primitive,

return:: a ->IO a

which does just that.

You might see do-notation all over the place in real Haskell programs. In
do-notation, our example program might look like:

main =doputStrLn"Hello, what is your name?"
name <-getLineputStrLn("Hello, "++ name ++"!")

This is in fact entirely equivalent to the above form, and is
translated into it by the Haskell compiler. So whenever you see a do
block, you can just imagine a chain of applications of (>>) and (>>=),
and some lambdas when appropriate to capture the results of actions. An action on its own on a line in a do-block will be executed, and a line of the form v <- x will cause the action x to be run, and the result bound to the variable v.

Note that there is no function:

unsafe ::IO a -> a

as this would defeat the referential transparency of Haskell --
applying unsafe to the same IO action might return different things
every time, and Haskell functions aren't allowed to behave that way.

Now, I haven't really told you anything about monads in general yet.
Most monads are actually rather unlike IO, but they do share the
similar concepts of bind and return. For more on monads in general,
see my Monads as Containers article.