Personal tools

Introduction to IO

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
m (Formatted IO_Inside link)
m
Line 104: Line 104:
 
- [[User:CaleGibbard|CaleGibbard]]
 
- [[User:CaleGibbard|CaleGibbard]]
   
If you need comprehensive manual on using IO monad, look at the
+
  +
Footnote: if you need comprehensive manual on using IO monad, look at the
 
[http://haskell.org/haskellwiki/IO_inside Haskell I/O inside: Down the Rabbit's Hole]
 
[http://haskell.org/haskellwiki/IO_inside Haskell I/O inside: Down the Rabbit's Hole]
   

Revision as of 09:27, 4 July 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 :: IO String
putStrLn :: 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 = do putStrLn "Hello, what is your name?"
          name <- getLine
          putStrLn ("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.

A common mistake is to put something other than an action in the place of x, usually some other value. If you want to make a variable binding inside a do-block which doesn't involve running an action, then you can use a line of the form let a = b, which, like an ordinary let-expression will define a to be the same as b, but the definition scopes over the remainder of the do-block.

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.

- CaleGibbard


Footnote: if you need comprehensive manual on using IO monad, look at the Haskell I/O inside: Down the Rabbit's Hole