Monad composition

Tom Bevan tom@regex.com.au
24 Jan 2002 17:13:24 +1100


--=-iMQCvWOvg5myUMJzgSi7
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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 <tom@regex.com.au>
> To: Haskell Cafe List <haskell-cafe@haskell.org>
> 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
> > Haskell-Cafe@haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> > 
> 


--=-iMQCvWOvg5myUMJzgSi7
Content-Disposition: attachment; filename=combMonad.hs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1

import Termcap
import IO
import Monad


data State a b  =3D State ( a -> ( b , a ) )

instance Monad (State a) where
 return b =3D State ( \a -> ( b , a ) )
 (State st) >>=3D f =3D State ( \a -> let=20
 				  	( b , a' ) =3D st a
 			  	  	(State trans) =3D f b
	                 	  in=20
				  trans a' )


retrieve :: ( a -> b ) -> State a b
retrieve f =3D State ( \a -> ( f a , a ) )

update :: ( a -> a ) -> State a ()
update f =3D State ( \a -> ( () , f a ) )

input :: IO ( State a Char )

input =3D 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 =3D (fst.trans) init=20



stateAction :: State a b -> IO ( State a b )
stateAction act =3D return ( do act )

extr =3D stateAction.retrieve
upd =3D stateAction.update


finalise :: State a b -> a -> b

finalise (State trans) a =3D trans a

--=-iMQCvWOvg5myUMJzgSi7--