Continuations in scala (incomplete version)

21
continuations in fujohnwang @Twitter @weibo Saturday, July 9, 2011

description

a simple introduction to scala's continuation support, not thorough enough.

Transcript of Continuations in scala (incomplete version)

Page 1: Continuations in scala (incomplete version)

continuations in

fujohnwang@Twitter

@weibo

Saturday, July 9, 2011

Page 2: Continuations in scala (incomplete version)

what’s continuation?

Continuation capture the state of a computation(maybe to be invoked later).

Simple concept, but hard to get.

first-class support in schema language

Saturday, July 9, 2011

Page 3: Continuations in scala (incomplete version)

know CPS first

normal control flow structure

control flow structure after cps

PS.  code  sample  borrowed  from  http://www.slideshare.net/openbala/continuations

Saturday, July 9, 2011

Page 4: Continuations in scala (incomplete version)

WHat’s the big deal?

Saturday, July 9, 2011

Page 5: Continuations in scala (incomplete version)

usage scenarios

programming design pattern

coroutine

exception handling

web frameworks

backtracking?

Saturday, July 9, 2011

Page 6: Continuations in scala (incomplete version)

observer pattern with continuation

Saturday, July 9, 2011

Page 7: Continuations in scala (incomplete version)

Coroutine?

see the blog of jim mcbeatch

Saturday, July 9, 2011

Page 8: Continuations in scala (incomplete version)

Exception handling

No sample code yet

Saturday, July 9, 2011

Page 9: Continuations in scala (incomplete version)

Web Framework with continuation support

seaside / smalltalk

cocoon / Java

http://cocoon.apache.org/2.1/userdocs/flow/continuations.html

weblocks / common lisp

wee / ruby

ocsigen / ocaml

more...Saturday, July 9, 2011

Page 10: Continuations in scala (incomplete version)

Saturday, July 9, 2011

Page 11: Continuations in scala (incomplete version)

code pieces function calculator(){ var a, b, operator;

cocoon.sendPageAndWait("getA.html"); a = cocoon.request.get("a");

cocoon.sendPageAndWait("getB.html"); b = cocoon.request.get("b");

cocoon.sendPageAndWait("getOperator.html"); operator = cocoon.request.get("op");

try { if (operator == "plus") cocoon.sendPage("result.html", {result: a + b}); else if (operator == "minus") cocoon.sendPage("result.html", {result: a - b}); else if (operator == "multiply") cocoon.sendPage("result.html", {result: a * b}); else if (operator == "divide") cocoon.sendPage("result.html", {result: a / b}); else cocoon.sendPage("invalidOperator.html", {operator: operator}); } catch (exception) { cocoon.sendPage("error.html", {message: "Operation failed: " + exception.toString()}); }}Saturday, July 9, 2011

Page 12: Continuations in scala (incomplete version)

Continuation types

full continuation(aka. first-class cont.)

delimited continuation (aka. partial, composable cont.)

write async code as sequence one

mix cps code and normal code seamlessly

Saturday, July 9, 2011

Page 13: Continuations in scala (incomplete version)

What DC looks like?

  def  is123(n:Int):Boolean  =  {         reset  {             shift  {  k  :  (Int=>String)  =>                   (k(n)  ==  "123")             }.toString         }   }

reset  {        println(1)        shift  {  cont  =>  }        println(2)}//prints:  1

reset  {        println(1)        shift  {  cont  =>                println(2)        }        println(3)}//prints:  1  2

reset  {        println(1)        shift  {  cont  =>                cont()                println(2)        }        println(3)}//prints:  1  3  2

Saturday, July 9, 2011

Page 14: Continuations in scala (incomplete version)

first glance with dcreset and shift

reset sets up the boundary of dc(‘s cps)

shift captures the continuation

Result types

yield

return

Saturday, July 9, 2011

Page 15: Continuations in scala (incomplete version)

puzzle about yield and return

Saturday, July 9, 2011

Page 16: Continuations in scala (incomplete version)

scala cont features

compiler plugin + Library

compiler plugin does cps transformation

library supports control structures and directives

serializable (can be saved, distribute sys...)

see swarm?!

Others?

Saturday, July 9, 2011

Page 17: Continuations in scala (incomplete version)

available softwares

swarm

scalaflow

Akka’s DataFlow Concurrency library

nio actor implementation for akka

nioserver

Saturday, July 9, 2011

Page 18: Continuations in scala (incomplete version)

What I want to do with it

coroutine

combine with java nio

resource-efficient minimally-threaded networking layer framework

reduce hw cost finally

Saturday, July 9, 2011

Page 19: Continuations in scala (incomplete version)

Questions?

Saturday, July 9, 2011

Page 20: Continuations in scala (incomplete version)

Referenceshttp://www.scala-lang.org/node/2096

http://okmij.org/ftp/continuations/Continuations.html

http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html

http://lamp.epfl.ch/~rompf/continuations-icfp09.pdf

http://okmij.org/ftp/continuations/index.html

http://suereth.blogspot.com/2010/03/how-you-should-think-about-delimited.html

http://dcsobral.blogspot.com/2009/07/delimited-continuations-explained-in.html

http://blog.richdougherty.com/2009/02/delimited-continuations-in-scala_24.html

http://en.wikipedia.org/wiki/Seaside_(software)

http://en.wikipedia.org/wiki/Call-with-current-continuation

http://community.schemewiki.org/?call-with-current-continuation-for-C-programmers

http://community.schemewiki.org/?call-with-current-continuation

http://en.wikipedia.org/wiki/Coroutine

http://cocoon.apache.org/2.1/userdocs/flow/continuations.html

https://github.com/rschildmeijer/loft

Saturday, July 9, 2011

Page 21: Continuations in scala (incomplete version)

Thanks

Saturday, July 9, 2011