[Haskell-cafe] Can we come out of a monad?

John Lato jwlato at gmail.com
Mon Aug 9 15:19:01 EDT 2010


> From: Alexey Khudyakov <alexey.skladnoy at gmail.com>
>
> On Fri, 30 Jul 2010 09:29:59 +0200
> Stefan Holdermans <stefan at vectorfabrics.com> wrote:
>
>>
> No I think here we breaking out from _arbitrary_ monad. If monadic
> function works for every monad then it must work for identity monad
> too. Here is simplest form of purify function:
>
>> purify2 :: (forall m . Monad m => m a) -> a
>> purify2 m = runIdentity m
>
> I wonder could this function be written without resorting to concrete
> monad

I don't find purify2 particularly helpful because I almost never want
to break out of any arbitrary monad; I want to be able to break out of
a specific monad without knowing which monad it is, that is:

purify3 :: Monad m => m a -> a
purify3 = undefined  --the only possible definition...

However, I just realized that something else is almost as good:

evalCont :: Cont r a -> r
evalCont k = runCont k id

As Cont is the "Mother of all monads", it can be used to emulate the
behavior of any other monad.  If you had a library with instances of
MonadReader, MonadWriter, MonadState, etc. (or operational
equivalents) defined for the Cont monad, then you would have a purify
function that works for all interesting monads (except IO, STM, and
maybe ST), which is almost as good.

John


More information about the Haskell-Cafe mailing list