Haskell/Simple input and output

Back to the real world
Beyond internally calculating values, we want our programs to interact with the world. The most common beginners' program in any language simply displays a "hello world" greeting on the screen. Here's a Haskell version:

is one of the standard Prelude tools. As the "putStr" part of the name suggests, it takes a  as an argument and prints it to the screen. We could use  on its own, but we usually include the "Ln" part so to also print a line break. Thus, whatever else is printed next will appear on a new line.

So now you should be thinking, "what is the type of the putStrLn function?" It takes a  and gives… um… what? What do we call that? The program doesn't get something back that it can use in another function. Instead, the result involves having the computer change the screen. In other words, it does something in the world outside of the program. What type could that have? Let's see what GHCi tells us:

"IO" stands for "input and output". Wherever there is  in a type, interaction with the world outside the program is involved. We'll call these  values actions. The other part of the  type, in this case , is the type of the return value of the action; that is, the type of what it gives back to the program (as opposed to what it does outside the program). (pronounced as "unit") is a type that only contains one value also called  (effectively a tuple with zero elements). Since  sends output to the world but doesn't return anything to the program,   is used as a placeholder. We might read  as "action which returns  ".

A few more examples of when we use IO:


 * print a string to the screen
 * read a string from a keyboard
 * write data to a file
 * read data from a file

What makes IO actually work? Lots of things happen behind the scenes to take us from  to pixels in the screen, but we don't need to understand any of the details to write our programs. A complete Haskell program is actually a big IO action. In a compiled program, this action is called  and has type. From this point of view, to write a Haskell program is to combine actions and functions to form the overall action  that will be executed when the program is run. The compiler takes care of instructing the computer on how to do this.

Sequencing actions with do
do notation provides a convenient means of putting actions together (which is essential in doing useful things with Haskell). Consider the following program:

Before we get into how do works, take a look at. It goes to the outside world (to the terminal in this case) and brings back a. What is its type?

That means  is an IO action that, when run, will return a. But what about the input? While functions have types like  which reflect that they take arguments and give back results,   doesn't actually take an argument. It takes as input whatever is in the line in the terminal. However, that line in the outside world isn't a defined value with a type until we bring it into the Haskell program.

The program doesn't know the state of the outside world until runtime, so it cannot predict the exact results of IO actions. To manage the relationship of these IO actions to other aspects of a program, the actions must be executed in a predictable sequence defined in advance in the code. With regular functions that do not perform IO, the exact sequencing of execution is less of an issue — as long as the results eventually go to the right places.

In our name program, we're sequencing three actions: a  with a greeting, a , and another. With the, we use   notation which assigns a variable name to stand for the returned value. We cannot know what the value will be in advance, but we know it will use the specified variable name, so we can then use the variable elsewhere (in this case, to prepare the final message being printed). The final action defines the type of the whole  block. Here, the final action is the result of a, and so our whole program has type.

Left arrow clarifications
While actions like  are almost always used to get values, we are not obliged to actually get them. For example, we could write something like this:

In this case, we don't use the input at all, but we still give the user the experience of entering their name. By omitting the, the action will happen, but the data won't be stored or accessible to the program.

can be used with any action except the last
There are very few restrictions on which actions can have values obtained from them. Consider the following example where we put the results of each action into a variable (except the last... more on that later):

The variable  gets the value out of its action, but that isn't useful in this case because the action returns the unit value. So while we could technically get the value out of any action, it isn't always worth it.

So, what about the final action? Why can't we get a value out of that? Let's see what happens when we try:

Making sense of this requires a somewhat deeper understanding of Haskell than we currently have. Suffice it to say, after any line where you use  to get the value of an action, Haskell expects another action, so the final action cannot have any  s.

Controlling actions
Normal Haskell constructions like if/then/else can be used within the do notation, but you need to take some care here. For instance, in a simple "guess the number" program, we have:

Remember that the if/then/else construction takes three arguments: the condition, the "then" branch, and the "else" branch. The condition needs to have type , and the two branches can have any type, provided that they have the same type. The type of the entire if/then/else construction is then the type of the two branches.

In the outermost comparison, we have  as the condition. That has the correct type. Let's now consider the "then" branch. The code here is:

Here, we are sequencing two actions:  and . The first has type, which is fine. The second also has type, which is fine. The type result of the entire computation is precisely the type of the final computation. Thus, the type of the "then" branch is also. A similar argument shows that the type of the "else" branch is also. This means the type of the entire if/then/else construction is, which is what we want.

Note: be careful if you find yourself thinking, "Well, I already started a do block; I don't need another one." We can't have code like:

Here, since we didn't repeat the do, the compiler doesn't know that the  and   calls are supposed to be sequenced, and the compiler will think you're trying to call with three arguments: the string, the function and the integer, and thus reject the program.

Actions under the microscope
Actions may look easy up to now, but they are a common stumbling block for new Haskellers. If you have run into trouble working with actions, see if any of your problems or questions match any of the cases below. We suggest skimming this section now, then come back here when you actually experience trouble.

Mind your action types
One temptation might be to simplify our program for getting a name and printing it back out. Here is one unsuccessful attempt:

Let us boil the example above down to its simplest form. Would you expect this program to compile?

For the most part, this is the same (attempted) program, except that we've stripped off the superfluous "What is your name" prompt as well as the polite "Hello". One trick to understanding this is to reason about it in terms of types. Let us compare: We can use the same mental machinery we learned in ../Type basics/ to figure how this went wrong. is expecting a  as input. We do not have a ; we have something tantalisingly close: an. This represents an action that will give us a  when it's run. To obtain the  that   wants, we need to run the action, and we do that with the ever-handy left arrow,.

Now the name is the String we are looking for and everything is rolling again.

Mind your expression types too
So, we've made a big deal out of the idea that you can't use actions in situations that don't call for them. The converse of this is that you can't use non-actions in situations that expect actions. Say we want to greet the user, but this time we're so excited to meet them, we just have to SHOUT their name out:

This is similar to the problem we ran into above: we've got a mismatch between something expecting an IO type and something which does not produce IO. This time, the trouble is the left arrow ; we're trying to left-arrow a value of , which really isn't left arrow material. It's basically the same mismatch we saw in the previous section, except now we're trying to use regular old String (the loud name) as an IO String. The latter is an action, something to be run, whereas the former is just an expression minding its own business. We cannot simply use  because a   sequences actions, and   is not an action.

So how do we extricate ourselves from this mess? We have a number of options:
 * We could find a way to turn  into an action, to make it return  . However, we don't want to make actions go out into the world for no reason. Within our program, we can reliably verify how everything is working. When actions engage the outside world, our results are much less predictable. An IO   would be misguided. Consider another issue too: what if we wanted to use makeLoud from some other, non-IO, function? We really don't want to engage IO actions except when absolutely necessary.
 * We could use a special code called  to promote the loud name into an action, writing something like  . This is slightly better. We at least leave the   function itself nice and IO-free whilst using it in an IO-compatible fashion. That's still moderately clunky because, by virtue of left arrow, we're implying that there's action to be had -- how exciting! -- only to let our reader down with a somewhat anticlimactic   (note: we will learn more about appropriate uses for   in later chapters).
 * Or we could use a let binding...

It turns out that Haskell has a special extra-convenient syntax for let bindings in actions. It looks a little like this:

If you're paying attention, you might notice that the let binding above is missing an. This is because  bindings inside   blocks do not require the   keyword. You could very well use it, but then you'd have messy extra do blocks. For what it's worth, the following two blocks of code are equivalent.

Learn more
At this point, you have the fundamentals needed to do some fancier input/output. Here are some IO-related topics you may want to check in parallel with the main track of this course.
 * You could continue the sequential track, learning more about types and eventually monads.
 * Alternately: you could start learning about building graphical user interfaces in the ../GUI/ chapter
 * For more IO-related functionality, you could also consider learning more about the System.IO library