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

Kevin Jardine kevinjardine at gmail.com
Fri Jul 30 06:06:53 EDT 2010

I think that we are having a terminology confusion here. For me, a
pure function is one that does not operate inside a monad. Eg. ++,
map, etc.

It was at one point my belief that although code in monads could call
pure functions, code in pure functions could not call functions that
operated inside a monad.

I was then introduced to functions such as execState and
unsafePerformIO which appear to prove that my original belief was

Currently I am in a state of deep confusion, but that is OK, because
it means that I am learning something new!


On Jul 30, 11:55 am, Anton van Straaten <an... at appsolutions.com>
> 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
> function.
> Anton
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-C... at haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list