Introduction to Haskell IO/Actions
(still entering content for first draft.)
Revision as of 20:50, 16 December 2006
When we're programming in Haskell and we want to do something that has a side effect, something that affects the world in some way, we use actions. Actions are values in the Haskell language, much like the number three, thestring
names, passed into a function as an argument or be the result of a function. Like all other Haskell values, every action has a type. There are many kinds of actions but we'll start with a very important one called an IO action. These are the actions that can change the world outside of the programming. Here are some examples of IO actions:
- Print the string "hello" to the console.
- Read a line of input from the console.
- Establish a network connection to www.google.com on port 80.
- Read two lines of input from the terminal, interpret them as numbers, add them together and print out the result.
- A first-person shooter game that uses mouse movements as input and renders graphics to the screen.
As you can see, IO actions range from the very simple (printing a string) to very comlex (a video game). You may have also noticed that IO actions can also result in a value that can be used by the Haskell program. The point of reading a line of input from the console is to provide data to the program. The type of an action reflects the kind of action (IO) as well as the type of value that it provides as a result (for example String). We say that the action that reads a line of input from the console has the type IO String. In fact, all IO actions will have a type IO a for some result type a. When an action doesn't provide any useful data back to the program the unit type (written ()) is used to denote the result. For programmers familiar with C, C++ or Java, this is similar to the return type of "void" in those languages. The IO actions mentioned above have the following types:
- Print the string "hello" to the console: IO ()
- Read a line of input from the console: IO String
- Establish a network connection to www.google.com on port 80: IO Socket
- Read two lines of input from the terminal, interpret them as numbers, add them together and print out the result: IO Int
- A first-person shooter game that uses mouse movements as input and renders graphics to the screen: IO ()
While actions can result in values that are used by the program, they do not take any arguments. Consider putStrLn. It has the following type:
putStrLn :: String -> IO ()
that takes one argument (a string) and returns an action of type IO ().So putStrLn is not an action, but
subtle but important. All IO actions are of type IO a for some type a. They will never require additional arguments, although a function whichmakes the action (such as
around doesnt make anything happen. For example,
Actions are like directions. They specify something that can be done. They are not active in and of themselves. They need to be "run" to make something happen. Simply having an action lying
in haskell that prints the line "hello". Ithas type
x = putStrLn "hello"
but that doesn't cause the haskell program to print out "hello"! Haskell only runs one IO action in a program, the action called main. Thisaction should have the type
will print out "hello":
module Main where main :: IO () main = putStrLn "hello"
1 Do Notation
You may be wondering how any Haskell program can do anything useful if it can only run a single IO action. As we saw earlier, IO actions can be very complex. We can combine many simple actions together to form more complicated actions. To combine actions together we use a do-block.
A do-block combines together two or more actions into a single action. When two IO actions are combined the result is an IO action that, when invoked, performs the first action and then performs the second action. Here's a simple example.
main :: IO () main = do putStrLn "hello" putStrLn "world"
Main is an action that prints a line "hello" and then prints a line "world". If the first action had any side effects, those effects are visible to the second action when it is performed. For example, if a file is written in the first action and read in the second action, the change to the file will be visible to the read. Remember that IO actions can return results to the program. The result of a do-block is the result of the last action in the do block. In our example above, the lastaction (
Do-blocks can also make use of the result of one action when constructing another action. For example:
main:: IO () main = do line <- getLine putStrLn ("you said: " ++ line)
takes its result and invokesthe action
Notice thatan arrow (
The arrow indicates that the result of an action is being bound. Thetype of
We've used do-blocks to combine two actions together. This provides enough power to combine more actions together:
main :: IO () main = do putStrLn "Enter two lines" do line1 <- getLine do line2 <- getLine putStrLn ("you said: " ++ line1 ++ " and " ++ line2)
Since the innermost do-block is an action, it can be combined with
action. Luckily we don't have to go through all this trouble. Do-blocks allow multiple actions to be specified in a single block. The meaning of these multi-action blocks is identical to the nested example above: the bindings are made visible to all successive actions. The previous example can be rewritten more compactly as
main :: IO () main = do putStrLn "Enter two lines" line1 <- getLine line2 <- getLine putStrLn ("you said: " ++ line1 ++ " and " ++ line2)
Of course we are free to use other Haskell language features when writingour program. Instead of putting all of our actions in
we may want to factor some common operations out as separate actions or functions that build actions. For example, we may want to combine prompting and user input:
promptLine :: String -> IO String pormptLine prompt = do putStr prompt getLine main :: IO () main = do line1 <- promptLine "Enter a line: " line2 <- promptLine "And another: " putStrLn ("you said: " ++ line1 ++ " and " ++ line2)
string without a newline character) and reads a line from the console.The result of the action is the result of the last action,
Let's try to write a slightly more helper function that reads two lines and returns both of them concatenated together:
promptTwoLines :: String -> String -> IO String promptTwoLines prompt1 prompt2 = do line1 <- promptLine prompt1 line2 <- promptLine prompt2 line1 ++ " and " ++ line2 -- ??
There's a problem here. We know how to prompt for and read in both lines of input, and we know how to combine those lines of input, but we don't have an action that results in the combined string. Remember, do-blocks combine together actions and the result of the do-blockis the result of the last action.
is a string, not an action resulting in a string and so cannot be used as the last line of the do-block. What we need is a way to make an action that results in a particular value. This is exactlywhat the
takes any type of value and makes an action that results in that value. We can now complete our helper:
promptTwoLines :: String -> String -> IO String promptTwoLines prompt1 prompt2 = do line1 <- promptLine prompt1 line2 <- promptLine prompt2 return (line1 ++ " and " ++ line2) main :: IO () main = do both <- promptTwoLines "First line: " "Second line: " putStrLn ("you said " ++ both)
Here's a very important point that many beginners get confused about: "return" does not affect the control flow of the program! Return does not break the execution of the do-block. Return may occasionally be used in the middle of a do-block where it doesn't directly contribute to the result of the do-block. Return is simply a function that makes an action whose result is a particular value. In a sense it wraps up a value into an action.
We can also use Haskell's control flow features such as if-then-else, case-of or recursion with actions. For example:
main :: IO () main = do line <- promptLine "What do you want? " if line == "wisdom" then putStrLn "No man is without enemies." else putStrLn ("I don't have any " ++ line)
boolean value.Here we used
combines into a larger action. If we want several actions to be performedwithin a
combine them into a single action using another do-block:
main :: IO () main = do line <- promptLine "What do you want? " if line == "wisdom" then putStrLn "No man is without enemies." else do putStrLn ("I don't have any " ++ line) putStrLn "Perhaps you want some wisdom?"
Let-bindings are also available within do-blocks, for example:
main :: IO () main = do line <- promptLine "Enter a value: " let line2 = "\"" ++ line ++ "\"" in do putStrLn ("you said " ++ line2) putStrLn "Bye."
When working in do-blocks there is a more convenient syntax for using
any nesting. When using this syntax, the scope of the bound variable includes the remainder of the do-block, just as the scope of do-bindings (using arrows) within the do-block. The previous example can be written more compactly as:
main :: IO () main = do line <- promptLine "Enter a value: " let line2 = "\"" ++ line ++ "\"" putStrLn ("you said " ++ line2) putStrLn "Bye."
Let-bindings and do-bindings can be freely interspersed within the do-block.
2 There's no escape
There's one final detail about IO actions that you should be aware of: there is no escape! The only way to get a result from an IO action isto invoke the IO action (through
used to affect the outside world through another IO action. There is no way to take an IO action and extract just its results to a simplevalue (an inverse-
action's results appear unwrapped are within a do-block.
Having said that, I have a confession to make: you will probably find out later that this is not entirely true. There are some rare exceptions that can be used in extreme circumstances to escape from IO, but there are good reasons why you will not be using them.
- IO actions are used to affect the world outside of the program.
- Actions take no arguments but have a result value.
- Actions are inert until run. Only one IO action in a Haskell program is run ().main
- Do-blocks combine multiple actions together into a single action.
- Combined IO actions are executed sequentially with observable side-effects.
- Arrows are used to bind action results in a do-block.
- Return is a function that builds actions. It is not a form of control flow!