Adding a Monad Instance

Before we dive into what we can do with Monad, let’s start by writing out our instance. You might recall that the Monad type class requires that we implement two functions: return and (>>=). When you already have an Applicative instance, like we do, it’s usually to define return in terms of pure. So, we’ll start by defining our new class:

 instance​ ​Monad​ ​FilePackParser​ ​where
  return = pure

Next we need to define (>>=). As you may recall, the general type of (>>=) is:

 (>>=) :: m a -> (a -> m b) -> m b

Or, specialized to FilePackParser:

 (>>=) :: ​FilePackParser​ a -> (a -> ​FilePackParser​ b) -> ​FilePackParser​ b

We’ll start out defining a new FilePackParser and binding its input so that we can use it inside ...

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.