lightweight session programming in scala
play

Lightweight Session Programming in Scala Alceste Scalas Nobuko - PowerPoint PPT Presentation

t i f a c r t A C o m p * t * l e * n t e e * A s t P i W s E n e O o C l l C D O * o * e c C s u u m E e e E R n * o e t v t y s d d a E * a e u l t a Lightweight Session


  1. t i f a c r t A C o m p * t * l e * n t e e * A s t P i W s E n e O o C l l C D O * o * e c C s u u m E e e E R n * o e t v t y s d d a E * a e u l t a Lightweight Session Programming in Scala Alceste Scalas Nobuko Yoshida Theory and Applications of Behavioural Types Dagstuhl, February 1 st , 2017

  2. Introduction lchannels Formal properties Demo Conclusions App server Client Frontend Auth GetSession ( Id ) GetSession ( Id ) Scala, Akka Typed & CPS protocols Alt Alt Session Id is valid notoriously Success ( S ) (R. Kuhn, “Project G˚ albma: Actors vs Types”, slide 42) main Active ( S ) on- ommunic- ges. Alt Alt Session Id does not exist, or is expired Failure () resort- GetAuthentication () Actors interact via untyped mailboxes. Authentication ( A ) ell New ( A ) Non-trivial protocols must be checked at run-time of Authenticate ( Credentials ) the Alt Alt from Invalid credentials ✗ Failure () ced- fron- fron- Alt Alt Valid credentials client CreateSession ( User ) NewSession ( S ) which ✓ Success ( S ) the ron- er, The Loop/Alt i ∈ { 1 , . . . , n } Loop/Alt i ∈ { 1 , . . . , n } Client-server session an Command i ( T i ) Response i ( T ′ i ) authen- . . . . . . a who

  3. Introduction lchannels Formal properties Demo Conclusions App server Client Frontend Auth GetSession ( Id ) GetSession ( Id ) Scala, Akka Typed & CPS protocols Alt Alt Session Id is valid notoriously Success ( S ) (R. Kuhn, “Project G˚ albma: Actors vs Types”, slide 42) main Active ( S ) on- ommunic- ges. Alt Alt Session Id does not exist, or is expired Failure () resort- GetAuthentication () Actors interact via untyped mailboxes. Authentication ( A ) ell New ( A ) Non-trivial protocols must be checked at run-time of Authenticate ( Credentials ) the Alt Alt from Invalid credentials ✗ Failure () ced- fron- Akka Typed proposes: fron- Alt Alt Valid credentials client CreateSession ( User ) NewSession ( S ) which ✓ Success ( S ) the 1. typed references ActorRef[A] ron- er, 2. Continuation-Passing Style protocols The Loop/Alt i ∈ { 1 , . . . , n } Loop/Alt i ∈ { 1 , . . . , n } Client-server session an Command i ( T i ) Response i ( T ′ i ) authen- . . . . . . a who

  4. Introduction lchannels Formal properties Demo Conclusions CPS protocols in Akka Typed (cont’d) (R. Kuhn, “Project G˚ albma: Actors vs Types”, slide 42) case class GetSession(id: Int, replyTo: ActorRef[GetSessionResult]) sealed abstract class GetSessionResult Client Frontend Auth App server case class Active(service: ActorRef[Command]) extends GetSessionResult GetSession ( Id ) case class New(authc: ActorRef[Authenticate]) GetSession ( Id ) extends GetSessionResult Alt Alt // ... case classes for authentication, etc ... Session Id is valid notoriously Success ( S ) main Active ( S ) on- ommunic- ges. Alt Alt Session Id does not exist, or is expired Failure () resort- GetAuthentication () Authentication ( A ) ell New ( A ) of Authenticate ( Credentials )

  5. Introduction lchannels Formal properties Demo Conclusions CPS protocols in Akka Typed (cont’d) (R. Kuhn, “Project G˚ albma: Actors vs Types”, slide 42) case class GetSession(id: Int, replyTo: ActorRef[GetSessionResult]) sealed abstract class GetSessionResult Client Frontend Auth App server case class Active(service: ActorRef[Command]) extends GetSessionResult GetSession ( Id ) case class New(authc: ActorRef[Authenticate]) GetSession ( Id ) extends GetSessionResult Alt Alt // ... case classes for authentication, etc ... Session Id is valid notoriously Success ( S ) main Active ( S ) on- To send a message, produce its continuation ommunic- (pseudo-code follows) ges. Alt Alt Session Id does not exist, or is expired Failure () resort- GetAuthentication () def client(frontend: ActorRef[GetSession]) = { Authentication ( A ) val cont = spawn[GetSessionResult] { ell New ( A ) case New(a) => doAuthentication(a) of case Active(s) => doSessionLoop(s) Authenticate ( Credentials ) } frontend ! GetSession(42, cont) }

  6. Introduction lchannels Formal properties Demo Conclusions CPS protocols: opportunities and limitations case class GetSession(id: Int, replyTo: ActorRef[GetSessionResult]) sealed abstract class GetSessionResult case class Active(service: ActorRef[Command]) extends GetSessionResult case class New(authc: ActorRef[Authenticate]) extends GetSessionResult case class Authenticate(username: String, password: String, replyTo: ActorRef[AuthenticateResult]) sealed abstract class AuthenticateResult case class Success(service: ActorRef[Command]) extends AuthenticateResult case class Failure() extends AuthenticateResult sealed abstract class Command // ... case classes for the client-server session loop ... CPS protocols are Scala types yielding structured interaction in Akka But they are also: ▸ low-level , cumbersome to write (and read) ▸ not related with any high-level protocol formalism ▸ ambiguous about reusability of (typed) actor references

  7. Introduction lchannels Formal properties Demo Conclusions Our approach Desiderata : ▸ find a formal link between CPS protocols and session types ▸ represent sessions in a language without session primitives ▸ lightweight : no language extensions, minimal dependencies

  8. Introduction lchannels Formal properties Demo Conclusions Our approach Desiderata : ▸ find a formal link between CPS protocols and session types ▸ represent sessions in a language without session primitives ▸ lightweight : no language extensions, minimal dependencies Inspiration (from concurrency theory): ▸ encoding of session types into linear types for π -calculus (Dardha, Giachino & Sangiorgi, PPDP’12)

  9. Introduction lchannels Formal properties Demo Conclusions Our approach Desiderata : ▸ find a formal link between CPS protocols and session types ▸ represent sessions in a language without session primitives ▸ lightweight : no language extensions, minimal dependencies Inspiration (from concurrency theory): ▸ encoding of session types into linear types for π -calculus (Dardha, Giachino & Sangiorgi, PPDP’12) Result: Lightweight Session Programming in Scala

  10. Introduction lchannels Formal properties Demo Conclusions lchannels : interface abstract class In[+A] { def receive(implicit d: Duration): A } abstract class Out[-A] { def send(msg: A): Unit } API reminds standard Promise s/ Future s ▸ similar runtime linearity checks and error handling Note input/output co/contra-variance

  11. Introduction lchannels Formal properties Demo Conclusions lchannels : interface abstract class In[+A] { def receive(implicit d: Duration): A def ?[B](f: A => B)(implicit d: Duration): B = { f(receive) } } abstract class Out[-A] { def send(msg: A): Unit def !(msg: A) = send(msg) } API reminds standard Promise s/ Future s ▸ similar runtime linearity checks and error handling Note input/output co/contra-variance

  12. Introduction lchannels Formal properties Demo Conclusions lchannels : interface abstract class In[+A] { def future: Future[A] def receive(implicit d: Duration): A = { Await.result[A](future, d) } def ?[B](f: A => B)(implicit d: Duration): B = { f(receive) } } abstract class Out[-A] { def promise[B <: A]: Promise[B] // Impl. must be constant def send(msg: A): Unit = promise.success(msg) def !(msg: A) = send(msg) } API reminds standard Promise s/ Future s ▸ similar runtime linearity checks and error handling Note input/output co/contra-variance

  13. Introduction lchannels Formal properties Demo Conclusions lchannels : interface abstract class In[+A] { def future: Future[A] def receive(implicit d: Duration): A = { Await.result[A](future, d) } def ?[B](f: A => B)(implicit d: Duration): B = { f(receive) } } abstract class Out[-A] { def promise[B <: A]: Promise[B] // Impl. must be constant def send(msg: A): Unit = promise.success(msg) def !(msg: A) = send(msg) def create[B](): (In[B], Out[B]) // Used to continue a session } API reminds standard Promise s/ Future s ▸ similar runtime linearity checks and error handling Note input/output co/contra-variance

  14. Introduction lchannels Formal properties Demo Conclusions Session programming = In[ ⋅ ] / Out[ ⋅ ] + CPS protocols How do we instantiate the In[ ⋅ ] / Out[ ⋅ ] type parameters ? Client Server Session types S S Scala types In[?] or Out[?] Out[?] or In[?]

  15. Introduction lchannels Formal properties Demo Conclusions Session programming = In[ ⋅ ] / Out[ ⋅ ] + CPS protocols How do we instantiate the In[ ⋅ ] / Out[ ⋅ ] type parameters ? Client Server Session types S S CPS protocol classes Scala types A1 , A2 ,..., An In[A] or Out[A] Out[A] or In[A]

  16. Introduction lchannels Formal properties Demo Conclusions Session programming = In[ ⋅ ] / Out[ ⋅ ] + CPS protocols How do we instantiate the In[ ⋅ ] / Out[ ⋅ ] type parameters ? Client Server Session types S S ? ( U ) or ! ( U ) U ! ( U ) or ? ( U ) Linear I/O types CPS protocol classes Scala types A1 , A2 ,..., An In[A] or Out[A] Out[A] or In[A]

  17. Introduction lchannels Formal properties Demo Conclusions Programming with lchannels (I) S = µ X . ( ! Greet ( String ) . ( ? Hello ( String ) . X & ? Bye ( String ) . end ) ⊕ ! Quit . end )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend