[Haskell-cafe] My try for a CoroutineT monad tranformer

Dan Weston westondan at imageworks.com
Fri Apr 25 14:49:12 EDT 2008


I guess like minds think alike! See the very recent e-mail thread 
started by Ryan Ingram:
http://thread.gmane.org/gmane.comp.lang.haskell.cafe/39155/focus=39159

Take a look at the code referenced in Luke Palmer's reply:
http://luqui.org/git/?p=luqui-misc.git;a=blob;f=work/code/haskell/frp/Fregl/Suspend.hs

A snippet follows:

 > class (Monad m) => MonadSuspend v m | m -> v where
 >   attempt :: m a -> m (Either a (v -> m a))
 >   suspend :: m v
 >
 > newtype SuspendT v m a
 >   = SuspendT { runSuspendT :: m (Either a (v -> SuspendT v m a)) }

Your (Coroutine m a) appears to be isomorphic to (SuspendT () m a) [so 
Coroutine m () = SuspendT () m ()]

Your runCoroutineT appears to be isomorphic to a specialization of 
runSuspendT:

runSuspendT' :: SuspendT () m () -> m (Either () (() -> SuspendT () m ()))

Here the () -> a ~ a and Either () a ~ Maybe a

Dan

Joachim Breitner wrote:
> Hi,
> 
> (for those who follow planet.haskell.org this is old news, but I thought
> I’d tell the others)
> 
> In
> http://www.joachim-breitner.de/blog/archives/291-Pausable-IO-actions-for-better-GUI-responsiveness.html
> I describe how I wrote a monad transformer that allows me to pause a
> computation from within by returning another computation that I can use
> to re-start the computation (or to throw it away if I want). I needed
> this for a long running drawing computation in a gtk2hs program that I
> want to pause at convenient points (to allow user interaction), and that
> I need to abort when what I’m drawing is not up-to-date anymore.
> 
> The API basically consists of the function
>> runCoroutineT :: Monad m => CoroutineT m () -> m (Maybe (CoroutineT m ()))
> which runs the pausable computation, any Maybe returns Just the resume
> action, and the function
>> pause :: Monad m => CoroutineT m ()
> to be used inside the computation, which pauses it.
> 
> I have put the complete module in the darcs repository that might later
> also contain the GUI program at http://darcs.nomeata.de/FrakView/
> 
> What do you think of CoroutineT? Could it have been easily implemented
> using the existing monad transformers? Is it useful enough so that it
> should be included somewhere, and where? Are there any problems with
> strictness or other tripping points that I have overlooked?
> 
> Greetings,
> Joachim
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list