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

Kevin Jardine kevinjardine at gmail.com
Fri Jul 30 04:26:49 EDT 2010


Or is it possible to call a function in a monad and return a pure
result? I think that is what the original poster was asking?

I know that unsafePerformIO can do this, but I thought that was a bit
of a hack.

I'm still trying to understand how monads interact with types so I am
interested in this as well.

Kevin

On Jul 30, 10:11 am, Kevin Jardine <kevinjard... at gmail.com> wrote:
> Oops, I should have written
>
> IO ByteString
>
> as the State stuff is only *inside* execState.
>
> But a monad none the less?
>
> Kevin
>
> On Jul 30, 9:59 am, Kevin Jardine <kevinjard... at gmail.com> wrote:
>
> > The original poster states that the type of modifiedImage is "simply
> > ByteString" but given that it calls execState, is that possible?
>
> > Would it not be State ByteString?
>
> > Kevin
>
> > On Jul 30, 9:49 am, Anton van Straaten <an... at appsolutions.com> wrote:
>
> > > C K Kashyap wrote:
> > > > In the code here -
> > > >http://hpaste.org/fastcgi/hpaste.fcgi/view?id=28393#a28393
> > > > If I look at the type of modifiedImage, its simply ByteString - but
> > > > isn't it actually getting into and back out of the state monad? I am of
> > > > the understanding that once you into a monad, you cant get out of it? Is
> > > > this breaking the "monad" scheme?
>
> > > modifiedImage uses the execState function, which has the following type:
>
> > >    execState :: State s a -> s -> s
>
> > > In other words, it applies a State monad value to a state, and returns a
> > > new state.  Its entire purpose is to "run" the monad and obtain the
> > > resulting state.
>
> > > A monadic value of type "State s a" is a kind of delayed computation
> > > that doesn't do anything until you apply it to a state, using a function
> > > like execState or evalState.  Once you do that, the computation runs,
> > > the monad is "evaluated away", and a result is returned.
>
> > > The issue about not being able to escape that (I think) you're referring
> > > to applies to the functions "within" that computation.  A State monad
> > > computation typically consists of a chain of monadic functions of type
> > > (a -> State s b) composed using bind (>>=).  A function in that composed
> > > chain has to return a monadic value, which constrains the ability of
> > > such a function to escape from the monad.
>
> > > Within a monadic function, you may deal directly with states and
> > > non-monadic values, and you may run functions like evalState or
> > > execState which eliminate monads, but the function still has to return a
> > > monadic value in the end, e.g. using "return" to lift an ordinary value
> > > into the monad.
>
> > > Anton
> > > _______________________________________________
> > > Haskell-Cafe mailing list
> > > Haskell-C... at haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe
>
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-C... at haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-C... at haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list