Andre, I can't work out how it should be done. The way I see it, the StateIO monad should have four functions associated with it. 1/ update - a function to update the state 2/ retrieve - a function to retrieve the state from the monad These two are inherited from the standard State monad 3/ input - make a character from stndIn available to the state transformation functions 4/ output - send the state of the monad after a certain set of transformations to stndOut
I've managed to write functions 1-3 but not 4. Here's my work so far.I'm not really sure if this is on the right track. Tom On Thu, 2002-01-24 at 14:32, Andre W B Furtado wrote: > Hi, I have the same problem. Did anyone answered your question? > > Thanks, > -- Andre > > ----- Original Message ----- > From: Tom Bevan <[EMAIL PROTECTED]> > To: Haskell Cafe List <[EMAIL PROTECTED]> > Sent: Wednesday, January 23, 2002 4:29 AM > Subject: Monad composition > > > > > > Hi all, > > > > I'm writing a programme which requires IO actions to be interleaved with > > operations on a State monad. From what I can work out, this means that > > the IO Monad and the StateTransformation monad need to be composed into > > a single highr order monad. > > Does anyone have any references or pointers on how this should be done? > > > > > > Tom > > > > > > > > _______________________________________________ > > Haskell-Cafe mailing list > > [EMAIL PROTECTED] > > http://www.haskell.org/mailman/listinfo/haskell-cafe > > >
import Termcap import IO import Monad data State a b = State ( a -> ( b , a ) ) instance Monad (State a) where return b = State ( \a -> ( b , a ) ) (State st) >>= f = State ( \a -> let ( b , a' ) = st a (State trans) = f b in trans a' ) retrieve :: ( a -> b ) -> State a b retrieve f = State ( \a -> ( f a , a ) ) update :: ( a -> a ) -> State a () update f = State ( \a -> ( () , f a ) ) input :: IO ( State a Char ) input = do c <- getChar return ( State ( \a -> ( c , a ) ) ) -- This function sends the current state to standard out -- Somehow the state upto this point is calculated and -- fed to STDOUT. Can't work out how to express this -- in Haskell output :: ( a -> String ) -> IO ( State a () ) run :: State a b -> a -> b run (State trans) init = (fst.trans) init stateAction :: State a b -> IO ( State a b ) stateAction act = return ( do act ) extr = stateAction.retrieve upd = stateAction.update finalise :: State a b -> a -> b finalise (State trans) a = trans a