A Reader monad for multiple arguments?

285 views Asked by At

Is there an equivalent to the Reader monad that's equivalent to a -> b -> c rather than just a -> b. I know I could do (a, b) -> c but I'm not sure that's going to be very ergonomic.

1

There are 1 answers

3
Iceland_jack On

The only way to make that a monad is by wrapping it which may not be what you're after. The instance can be derived

{-# Language DerivingVia #-}

import Control.Monad.Reader

newtype Reader2 a b c = Reader2 (a -> b -> c)
 deriving (Functor, Applicative, Monad, MonadFix, MonadReader a)
 via ReaderT a ((->) b)

If you derive Representable it witnesses the isomorphism between Reader2 a b c and (a, b) -> c

index    :: Reader2 a b c -> ((a, b) -> c)
tabulate :: ((a, b) -> c) -> Reader2 a b c