Re: IO StateTransformer with an escape clause

2003-09-01 Thread Ganesh Sittampalam
On Tue, 26 Aug 2003 14:33:28 +1000, "Thomas L. Bevan"
<[EMAIL PROTECTED]> wrote:

>Hi,
>
>I'd like some help building an IO StateTransformer which can be escaped midway 
>through without losing the state accummulated up to that point.
>I tried constructing a
> StateT s MaybeIO a monad but the state is lost when the rest of 
>the monad collapses.

How is your MaybeIO type constructed? If with a monad transformer, then you
could consider putting the MaybeT transformer outside the StateT transformer
(I think that should work).

Ganesh
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: IO StateTransformer with an escape clause

2003-08-27 Thread Andrew J Bromage
G'day all.

On Tue, Aug 26, 2003 at 02:33:28PM +1000, Thomas L. Bevan wrote:

> I'd like some help building an IO StateTransformer which can be
> escaped midway through without losing the state accummulated up to
> that point.

A simple way to do this is to use a ReaderT monad stacked on top of IO,
where the reader state is an IORef to whatever your "real" state is, then
write wrapper functions to access the IORef like a state monad.

For example, if you had:

newtype PersistentStateT s m a
  = PersistentStateT (ReaderT (IORef s) m a)

then you could write:

-- WARNING: Unchecked code follows!

instance (MonadIO m) => MonadState s (PersistentState s m) where
get   = PersistentStateT $ do
r <- ask
liftIO (readIORef r)

set s = PersistentStateT $ do
r <- ask
liftIO (writeIORef r s)

The drawback is that the state is no longer backtrackable, say, if you
use a nondeterminism monad transformer stacked over this.

Cheers,
Andrew Bromage
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


IO StateTransformer with an escape clause

2003-08-27 Thread Thomas L. Bevan
Hi,

I'd like some help building an IO StateTransformer which can be escaped midway 
through without losing the state accummulated up to that point.
I tried constructing a
 StateT s MaybeIO a monad but the state is lost when the rest of 
the monad collapses.

Tom

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe