Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monad transformers with identity monad

Tags:

haskell

What's the point in using a Monad transformer with the Identity monad rather than just using the "standard" version of the transformer?

Is it more flexible?

like image 587
Matthew H Avatar asked Apr 11 '13 10:04

Matthew H


2 Answers

Back in mtl 1.0 we had both

newtype State s a = State { runState :: s -> (a, s) }

and

newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }

However, this meant anybody who had to implement instances for things like MonadState wound up duplicating effort.

In transformers (and the now defunct monads-fd and monads-tf) Ross Paterson decided to use the simpler approach of only offering the latter and using Identity as the base monad.

This led to reduced implementation effort in maintaining the mtl and removed the fact that there were two different ways to implement the State monad. It did, however, make the internals of the mtl harder to teach, because you need to understand the transformers versions right out of the gate and don't get the simplified version as training wheels.

When the old mtl was retired and monads-fd became mtl 2.0, using the existing transformers this design decision was carried over.

I personally liked having the separate simple monads for pedagogical purposes at least, but there were far more people on the other side of the debate.

like image 113
Edward Kmett Avatar answered Oct 15 '22 09:10

Edward Kmett


From the Documentation: Computationally, there is no reason to use the Identity monad instead of the much simpler act of simply applying functions to their arguments. The purpose of the Identity monad is its fundamental role in the theory of monad transformers. Any monad transformer applied to the Identity monad yields a non-transformer version of that monad.

As i understand it, getting the non-transformer version of a monad from a monad transformer by applying the identity monad is exactly the thing that the identity monad is there for. There is no advantage over just using the non-transformer monad, yet sometimes you have to use a monad transformer, e.g. when a function you want to use requires it.

like image 44
tauli Avatar answered Oct 15 '22 10:10

tauli