This is probably quite a naive question.
I’m writing an application, and up until this point, most of my functions have been written to take parameters that are not monads, and to return a monad, usually Either
. I am usually calling them on monads, using the monadic bind, or sequence
or similar. So something like
addOneToHead :: [Int] -> Maybe Int
addOneToHead [] = Nothing
addOneToHead (x:xs) = Just $ x + 1
main =
addOneToHead =<< (Just [1, 2])
The bulk of my functions are pure functions, with just a few involving the IO monad. This is all great and feels like a comfortable pattern to work with.
However, I’m now at the stage where I need some reasonable logging output, and, realistically, this means peppering my code with logInfo
function calls. I had faintly realized that this would involve expanding the use of the IO
monad, but hadn’t realized quite how much. There seem to be a few recommended patterns for logging - using a MonadLogger
package like Blammo
, using ReaderT
to pass around a log function, using RIO
- but a point of commonality between all of them is that most or all of your code is run within these monads. This means that almost every function starts with a do
, and is then written in do-syntax rather than conventional Haskell syntax.
I’ve seen some proposed methods for mitigating the potential impurities resulting from this (eg, https://www.fpcomplete.com/blog/readert-design-pattern/), and I’ve been looking at the RIO library, which seems neat but leans very heavily into this approach. Passing around a global state object via the ReaderT
pattern seems like a generally good idea. But before I go down this path - and rewrite almost all of my existing code - I thought I’d check I’m understanding this situation correctly. Can anyone tell me which of these statements is most correct:
- Most production Haskell doesn’t use logging, or just uses
Debug.Trace
opportunistically and so remains pure - Most production Haskell does use this pattern, and most production haskell functions are written in do-syntax
- What I’ve written above is a misunderstanding or is in some other way incorrect.
Thanks!