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

Anton van Straaten anton at appsolutions.com
Fri Jul 30 05:55:47 EDT 2010

Kevin Jardine wrote:
> I think that these are therefore the responses to the original
> questions:
>> I am of the understanding that once you into a monad, you cant get out of it?
> You can run monadic functions and get pure results. 

Some clarifications:

First, many monads (including State) are completely pure in a 
referential transparency sense, so the issue we're discussing is not a 
question of whether results are pure (in general) but rather whether 
they're monadic or not, i.e. whether the type of a result is something 
like "Monad m => m a", or just "a".

Second, what I was calling a "monadic function" is a function of type:

   Monad m => a -> m b

These are the functions that bind (>>=) composes.  When you apply these 
functions to a value of type a, you always get a monadic value back of 
type "m b", because the type says so.

These functions therefore *cannot* do anything to "escape the monad", 
and by the same token, a chain of functions composed with bind, or the 
equivalent sequence of statements in a 'do' expression, cannot escape 
the monad.

It is only the monadic values (a.k.a. actions) of type "m b" that you 
can usually "run" using a runner function specific to the monad in 
question, such as execState (or unsafePerformIO).

(Note that as Lyndon Maydwell pointed out, you cannot escape a monad 
using only Monad type class functions.)

> So it looks like in that sense you can "get out of it".

At this level, you can think of a monad like a function (which it often 
is, in fact).  After you've applied a function to a value and got the 
result, you don't need the function any more.  Ditto for a monad, except 
that for monads, the applying is usually done by a monad-specific runner 


More information about the Haskell-Cafe mailing list