The question says it all, really. I know a (Scala) Monad looks like this:
trait Monad[M[_]] { def pure[A](a : A) : M[A] def bind[A, B](ma : M[A], f : A => M[B]) : M[B] }
What does a Monad Transformer look like? And what are they used for?
Let's say I just want to use replicateM
from Scalaz;
scala> import scalaz._; import Scalaz._ import scalaz._ import Scalaz._ scala> some(4).replicateM[List](2) res20: Option[List[Int]] = Some(List(4, 4))
Now let's say, rather than having an Option[Int]
, I need to read an Int
value from a File
:
scala> val f = (_ : java.io.File) => some(1) f: (java.io.File) => Option[Int] = <function1>
So, I can treat this reader, as if it was a Monad?
scala> ReaderT(f).replicateM[List](2) <console>:16: error: value replicateM is not a member of scalaz.ReaderT[Option,java.io.File,Int] ReaderT(f).replicateM[List](2) ^
Err, no.
Apologies if this all seems stupid, I'm just trying to understand what lovely goodness my wrapping the File => Option[Int]
in a ReaderT
can actually buy me.
All monad transformers are instances of MonadTrans , and so lift is available for them all. There is a variant of lift specific to IO operations, called liftIO , which is the single method of the MonadIO class in Control. Monad. IO.
monads are used to address the more general problem of computations (involving state, input/output, backtracking, ...) returning values: they do not solve any input/output-problems directly but rather provide an elegant and flexible abstraction of many solutions to related problems.
The Reader monad (also called the Environment monad). Represents a computation, which can read values from a shared environment, pass values from function to function, and execute sub-computations in a modified environment. Using Reader monad for such computations is often clearer and easier than using the State monad.
What is a Monad? A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.
Monad transformers are type functions that when applied to a monad type, generate a new monad, that combines the behavior of both components.
E.g. in the xmonad
window manager, the computations run inside:
newtype X a = X (ReaderT XConf (StateT XState IO) a)
that is, a Reader
composed with a State
and an IO
monad.
Reader
gives access to read-only memoryState
provides a form of read-write stateIO
allows arbitrary external effectsNote that monad transforms are thus higher-rank types. They take a monadic type of kind (* -> *
), and yield a new type of that kind.
As always, the Haskell wiki has some useful content:
Where it all began:
Monad Transformers are used for combining / extending monads (add capabilities of one monad to another). E.g., ReaderT
(Reader Transformer) enriches given monad M
with Reader
capabilities (transforms given monad into Reader retaining original features of M
).
At the same time, Monad Transformers are normal monads that have bind
, return
, and other operations.
You can find examples of monad transformers in Scalaz - e.g., for Reader and State monads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With