6 Structuring programs with monad transformers

 

This chapter covers

  • Using monad transformers to extend monad functionality
  • Describing an application structure with monad stacks
  • Defining monad transformers
  • Overview of the most common monad transformers provided by the Haskell libraries

We’ve seen many monads already. We know that every monad specifies a way to combine two computations in a sequence by implementing the >>= operator. We also know that in a monadic setting we have a result of a computation with some additional effects: computations may fail (as in the Maybe monad) or give an opportunity to communicate with the real world by doing input and output (as in the IO monad), or we may mutate the state or consult an environment in some other effectful computations. There are many other monads, but we already know what all of them share in common.

In this chapter, we’ll discuss one simple question: how do we express computations that need to use more than one monadic effect at the same time? We’ll start with exploring the problem. We’ll solve a problem in one monad, find some flaws in that solution, and try to fix them. In Haskell it is often the case that there is a monad for this. But we have a monad already, so the problem now is how to add another monad to a solution. Let’s see.

6.1 The problem of combining monads

 

6.1.1 Evaluating expressions in reverse Polish notation

 
 
 
 

6.1.2 Introducing monad transformers and monad stacks

 
 
 

6.2 IO-based monad transformer stacks

 
 

6.2.1 Describing a monad stack

 
 

6.2.2 Exploiting monad stack functionality

 

6.2.3 Running an application

 

6.2.4 Can we do it without RWST?

 

6.3 What is a monad transformer?

 
 
 

6.3.1 Step 0: Defining a type for a transformer

 
 

6.3.2 Step 1: Turning a monad stack into a monad

 
 
 
 

6.3.3 Step 2: Implementing the full monad stack functionality

 
 
 
 

6.3.4 Step 3: Supplying additional functionality

 
 

6.3.5 Using a transformer

 

6.4 Monad transformers in the Haskell libraries

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage