Combining monadic effects
Before we solve the problem of combining different monadic effects in the standard Haskell way, let’s illustrate the problem on a small example application. We’ll also discuss the solution of writing a custom monad, which seems the most obvious but is not a best practice.
Logging revisited
Let’s make the logging functionality from the previous chapter a bit more sophisticated by adding several requirements. Recall that previously, we used the Writer [String]
monad for logging. To accommodate the new requirements, we will have to come up with a custom monad, which we’ll call App
.
These are the requirements:
- We want to distinguish different levels of importance for the logged messages:
data LogLevel = Mundane | Important | Critical deriving (Eq, Ord, Show)
- When we record a message in the log, we have to supply the logging level:
log :: LogLevel -> String -> App ()
It is convenient to set up a few...