[Haskell-cafe] Coroutines

Ryan Ingram ryani.spam at gmail.com
Thu Dec 18 05:26:26 EST 2008


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"


More information about the Haskell-Cafe mailing list