Coroutines

Andreas Gruenbacher gruenbacher-lists@geoinfo.tuwien.ac.at
Mon, 19 Mar 2001 18:43:55 +0100 (CET)


On Mon, 19 Mar 2001, Simon Peyton-Jones wrote:

> Lazy evaluation *is* coroutines, in a way.

What I had in mind was something like the following (pseudocode), which
could easily be implemented on top of coroutines:

class Channel c t where
	send    :: t -> c -> IO ()
	receive ::      c -> IO t

sender :: Int -> Channel -> ...
sender n receiver = do send n receiver
                       return (sender (n+1) receiver)

receiver :: Channel -> ...
receiver sender = do i <- receive sender
                     return (receiver sender)

c :: Channel Int
c = ...

main = run_them [ sender 1 c, receiver c ]


Maybe there is also a way to achieve something similar using higher order
functions---I just don't see how.


> Or you may be interested in Concurrent Haskell
> See section 4 of "Tackling the awkward squad"
> 	http://research.microsoft.com/~simonpj/papers/marktoberdorf.htm

Thanks, I'm chewing on this paper of yours since a couple of days already.
I particularly like the introduction to monads---it's the best I have seen
so far.


--Andreas.