One of the most common problems that people encounter when they first start trying to write larger Haskell is programs is understanding how to handle IO architecturally. Even when you understand the mechanics of how IO works in the language, it can be hard to figure out how to apply those ideas when you are building a complete program. The problem is that when you are coming from a language where you can do IO at any time, you get accustomed to writing programs that do IO as needed. If you visualize your program as a call graph, any individual function in the call graph might do a little bit of IO because it needs to look up a value from the environment, make a network request, or write a file. In many cases, this is even considered good architecture—encapsulating the implementation details of what type of IO a function is doing means that the user doesn’t need to know, or think about, any of those details.

In Haskell, this approach to designing applications can become a problem. The issue is that IO actions in Haskell aren’t regular values. An IO action can be combined with another IO action, but once we’ve introduced some IO we can’t go back to the world of normal pure functions. In some ways this is a terrible inconvenience, since most real applications will depend on a little bit of IO, and in fact that IO is often used to bootstrap the application. On the other hand, each call to IO is a potential failure scenario, and the inconvenience that we’re experiencing is often a case of Haskell focusing us to honestly face the risk of failure that was always there in our programs anyway.

Thankfully, the problem of structuring a pure functional program that needs to do some IO isn’t as hard as it might seem. There are several different approaches to managing applications that need to mix pure code with side effects. One of the most useful approaches is to make use of a common design pattern that is frequently called “Procedural Shell, Functional Core.”

The “Procedural Shell, Functional Core” is aptly named. The idea is that most of the IO that we do in our programs tends to happen at the “edges” of our application. That is to say, we typically need to do some IO to collect the data that we’re going to start processing in our program. Some examples of this are things like reading the command line arguments, reading files, or making some network requests. At the other end of our program, once it’s finished, we want to do something with the output. This might mean writing some data to the screen, writing a file, or sending some information to a database. This IO-heavy input and output forms the “procedural shell” of our application. Between these two parts of our program, we need to do some work on the input to translate it to the output. If we are careful to ensure that we are collecting all of our input up front, and we don’t write any output until the end, then this computation in the middle can be purely functional. This is the “functional core” of our program.

This pattern of having a procedural shell and a functional core doesn’t just happen once at the very top of our program though. As you’ll see in this chapter, we can sometimes build the larger procedural shell by composing IO actions that are themselves built using the same “procedural shell, functional core” approach. Although we still need to consume these IO actions as IO actions, by internally separating them out into procedural and functional pieces we can reason about, and test, the code more easily.