This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Delimited continuations and Scala

5 replies
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.

Judging from the comments on Rich Dougherty's blog post here,

http://blog.richdougherty.com/2009/03/goto-in-scala.html

it looks as tho' Tiark's CPS plugin is making a few people a little anxious.

On the contrary I think it's very exciting. Maybe this trivial example
implementing a pair of coroutines using shift/reset will seem a little
more compelling: look ... no threads!

import scala.continuations._
import scala.continuations.ControlContext._

object Test {

var producerCont : (Unit => Unit) = null
var consumerCont : (Int => Unit) = null

def produce(i : Int) : Unit @suspendable =
shift {
(k : Unit => Unit) => {
producerCont = k
consumerCont(i)
}
}

def consume : Int @ suspendable =
shift {
(k : Int => Unit) => {
consumerCont = k
if (producerCont != null)
producerCont()
}
}

def main(args: Array[String]) {
reset {
println("Consuming: "+consume)
println("Consuming: "+consume)
println("Consuming: "+consume)
}

reset {
println("Producing: 1")
produce(1)
println("Producing: 2")
produce(2)
println("Producing: 3")
produce(3)
}
}
}

Cheers,

Miles

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Delimited continuations and Scala

On Fri, Mar 20, 2009 at 10:31 PM, Miles Sabin wrote:
> On the contrary I think it's very exciting. Maybe this trivial example
> implementing a pair of coroutines using shift/reset will seem a little
> more compelling: look ... no threads!

I guess the output would be helpful ...

miles@lewis:continuations$ $SCALA_HOME/bin/scala -cp .:build/build.library/ Test
Producing: 1
Consuming: 1
Producing: 2
Consuming: 2
Producing: 3
Consuming: 3
miles@lewis:continuations$

Cheers,

Miles

Rich Dougherty
Joined: 2008-08-20,
User offline. Last seen 2 years 38 weeks ago.
Re: Delimited continuations and Scala

On Sat, Mar 21, 2009 at 11:31 AM, Miles Sabin wrote:
> Judging from the comments on Rich Dougherty's blog post here,
>
>  http://blog.richdougherty.com/2009/03/goto-in-scala.html
>
> it looks as tho' Tiark's CPS plugin is making a few people a little anxious.
>
> On the contrary I think it's very exciting. Maybe this trivial example
> implementing a pair of coroutines using shift/reset will seem a little
> more compelling: look ... no threads!

Hi Miles

Nice work with the coroutines! There are some amazing things that
become possible with delimited continuations. Implementing "goto" was
just a controversial example. Tiark has written some other examples,
including a task framework with fork/join, generators, a type-safe
printf and transparent non-blocking IO.

http://lampsvn.epfl.ch/trac/scala/browser/compiler-plugins/continuations...

Recently on the list, break and continue, and search with backtracking
have been discussed. (Admittedly one discussion was a bit lengthier
than the other.) Continuations could be used in both these examples
too.

http://www.nabble.com/continue-keyword-to22566052.html
http://www.nabble.com/Abstraction-for-Backtracking-to22548871.html

Continuations may not be the best solution to all these problems, but
they are definitely an option worth considering, and an option that
wasn't available before. By providing delimited continuations, Scala
makes it possible to implement powerful control flow operations
without needing to extend the core language - making Scala "scalable"
in yet another dimension.

Rich

--
http://www.richdougherty.com/

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Delimited continuations and Scala

On Fri, Mar 20, 2009 at 11:46 PM, Rich Dougherty wrote:
> Nice work with the coroutines! There are some amazing things that
> become possible with delimited continuations. Implementing "goto" was
> just a controversial example. Tiark has written some other examples,
> including a task framework with fork/join, generators, a type-safe
> printf and transparent non-blocking IO.

Making it easier to write non-blocking minimally-threaded IO automata
is a long-term project of mine. Seeing as I was on the original NIO
JSR and wrote the first NIO implementation (relative to a 1.3 JDK), I
find it more that a little amusing that Scala could very well end up
being the best language to actually use NIO from.

Cheers,

Miles

Tiark Rompf
Joined: 2009-02-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Delimited continuations and Scala

Hi,

it's cool to see you guys playing with the continuations plugin! And I
really like both of your examples as they show the kind of powerful
stuff that can be achieved with just a few lines of code. Thanks also
to you, Rich, for the trac tickets you submitted! Due to some other
high-priority tasks I haven't had the chance to look at them in detail
but I hope I'll manage to get back to it soon.

- Tiark

On 21.03.2009, at 00:57, Miles Sabin wrote:

> On Fri, Mar 20, 2009 at 11:46 PM, Rich Dougherty
> wrote:
>> Nice work with the coroutines! There are some amazing things that
>> become possible with delimited continuations. Implementing "goto" was
>> just a controversial example. Tiark has written some other examples,
>> including a task framework with fork/join, generators, a type-safe
>> printf and transparent non-blocking IO.
>
> Making it easier to write non-blocking minimally-threaded IO automata
> is a long-term project of mine. Seeing as I was on the original NIO
> JSR and wrote the first NIO implementation (relative to a 1.3 JDK), I
> find it more that a little amusing that Scala could very well end up
> being the best language to actually use NIO from.
>
> Cheers,
>
>
> Miles
>

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Delimited continuations and Scala

On Mon, Mar 23, 2009 at 11:15 PM, Tiark Rompf wrote:
> it's cool to see you guys playing with the continuations plugin!

BTW, I think you should advertise that it can be used in conjunction
with the interpreter,

miles@lewis:continuations$ $SCALA_HOME/bin/scala
-Xplugin:build/pack/selectivecps-plugin.jar -cp build/build.library

Welcome to Scala version 2.8.0.r17368-b20090324121215 (Java
HotSpot(TM) Server VM, Java 1.6.0_10).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.continuations._
import scala.continuations._

scala> import scala.continuations.ControlContext._
import scala.continuations.ControlContext._

scala> reset { shift { k : (Int => Int) => k(7) } }
res0: Int = 7

scala> reset { shift { k : (Int => Int) => k(7) } + 1 } * 2
res1: Int = 16

scala>

Cheers,

Miles

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland