Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a monad-transformer as distinct from a monad?

Tags:

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?


EDIT. Consider the following REPL session: if a monad transformer somehow decorates a monad with reader capabilities (or vice versa)

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.

like image 587
oxbow_lakes Avatar asked Feb 21 '11 12:02

oxbow_lakes


People also ask

Is every Monad transformer a Monad?

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.

Why is Monad useful?

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.

What is a reader Monad?

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 are monads Haskell?

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.


2 Answers

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 memory
  • State provides a form of read-write state
  • IO allows arbitrary external effects

Note 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:

  • Monad transformers
  • Monad transformers step by step: a tutorial.

Where it all began:

  • Functional Programming with Overloading and Higher-Order Polymorphism
like image 131
Don Stewart Avatar answered Oct 11 '22 05:10

Don Stewart


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.

like image 27
Vasil Remeniuk Avatar answered Oct 11 '22 03:10

Vasil Remeniuk