[Haskell-cafe] Coroutines
Bulat Ziganshin
bulat.ziganshin at gmail.com
Thu Dec 18 05:47:11 EST 2008
Hello Miguel,
Thursday, December 18, 2008, 1:42:21 PM, you wrote:
ruby doesn't support coroutines, but only iterators (where control
moved from caller to callee). usually control is on the caller side,
and coroutines gives control to both (or many) sides
coroutines are easily emulated in IO monad using multithreading, and i
think that are easily emulated both in Ruby and Haskell using callCC
> First thing I've tried when learning Ruby was something like that:
> ================
> def a
> yield {puts 1}
> end
> a {yield}
> ================
> It didn't work. Can Coroutine.hs do something like that?
> On 18 Dec 2008, at 13:26, Ryan Ingram wrote:
>> On Thu, Dec 18, 2008 at 2:00 AM, Nicolas Pouillard
>> <nicolas.pouillard at gmail.com> wrote:
>>> I don't see why one would need session types, channels... to
>>> express that.
>>> I maybe need a more complicated coroutines (ruby) example that
>>> would require
>>> using this system.
>>
>> OK, how would you type these routines in Haskell?
>>
>> def simple
>> yield "hello"
>> yield 1
>> yield (lambda { |x| x + 1 })
>> end
>>
>> def useSimple
>> state = 0
>> result = nil
>> simple { |x|
>> if (state == 0) then result = x
>> else if (state == 1) then result += (x * 4).toString
>> else if (state == 2) then result += x.call(10).toString
>> state = state + 1
>> }
>> result
>> end
>>
>> I know it's a bit contrived, but you get the idea.
>>
>> In Haskell using Control.Coroutine:
>>
>> simple :: forall rest. Session (String :!: Int :!: (Int -> Int) :!:
>> rest) rest ()
>> simple = do
>> put "hello"
>> put 1
>> put (\x -> x + 1)
>>
>> useSimple :: forall rest. Session (String :?: Int :?: (Int -> Int) :?:
>> rest) rest String
>> useSimple = do
>> string <- get
>> int <- get
>> func <- get
>> return (string ++ show (int * 4) ++ show (func 10))
>>
>> result :: String
>> result = snd $ connects simple useSimple
>> -- result = "hello411"
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Haskell-Cafe
mailing list