C. Varela1 Actors in SALSA and Erlang (PDCS 9, CPE 5*) Support for actor model in SALSA and Erlang Carlos Varela Rennselaer Polytechnic Institute October.

Slides:



Advertisements
Similar presentations
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Advertisements

The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Programming Paradigms for Concurrency Lecture 11 Part III – Message Passing Concurrency TexPoint fonts used in EMF. Read the TexPoint manual before you.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
ISBN Chapter 3 Describing Syntax and Semantics.
An Associative Broadcast Based Coordination Model for Distributed Processes James C. Browne Kevin Kane Hongxia Tian Department of Computer Sciences The.
C. Varela1 Actors (PDCS 4) AMST actor language syntax, semantics, join continuations Carlos Varela Rennselaer Polytechnic Institute April 16, 2015.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
C. Varela1 Typing and Parameter Passing Dynamic and Static Typing (EPL 4.1-4, VRH 2.8.3) Parameter Passing Mechanisms (VRH 6.4.4) Carlos Varela RPI.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Erlang concurrency. Where were we? Finished talking about sequential Erlang Left with two questions  retry – not an issue; I mis-read the statement in.
Scripting Languages For Virtual Worlds. Outline Necessary Features Classes, Prototypes, and Mixins Static vs. Dynamic Typing Concurrency Versioning Distribution.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
ITERATIVE COMPUTATIONS CONCURRENCY ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
FUNCTIONAL PROGRAMMING IN ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
Describing Syntax and Semantics
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC Lecture 8 (Multiple) Threads Semantics.
SALSA: Language and Architecture for Widely Distributed Actor Systems. Carlos Varela, Abe Stephens, Department of.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Supported by ELTE IKKK, Ericsson Hungary, in cooperation with University of Kent Erlang refactoring with relational database Anikó Víg and Tamás Nagy Supervisors:
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Eliminating Overlapping in Pattern Matching when Verifying Erlang Programs in muCRL Qiang Guo and John Derrick Department of Computer Science The University.
The Complexity of Distributed Algorithms. Common measures Space complexity How much space is needed per process to run an algorithm? (measured in terms.
Actors: a model of concurrent computation in Distributed Systems Amin Saremi Hamid Mohamadi.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSCI-383 Object-Oriented Programming & Design Lecture 12.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
C. Varela1 Distributed systems abstractions (PDCS 9, CPE 6*) Carlos Varela Rennselaer Polytechnic Institute October 20, 2015 * Concurrent Programming in.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
CMSC 330: Organization of Programming Languages Operational Semantics.
SystemC Semantics by Actors and Reduction Techniques in Model Checking Marjan Sirjani Formal Methods Lab, ECE Dept. University of Tehran, Iran MoCC 2008.
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
Functional Processing of Collections (Advanced) 6.0.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Chapter 2: Lambda Calculus
The need for Programming Languages
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Carlos Varela Rennselaer Polytechnic Institute November 1, 2016
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Java Primer 1: Types, Classes and Operators
Distributed systems abstractions (PDCS 9, CPE 6*)
Rennselaer Polytechnic Institute
Carlos Varela Rennselaer Polytechnic Institute November 3, 2017
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
Carlos Varela Rennselaer Polytechnic Institute October 6, 2017
Distributed (Systems) Programming Universal Actors, SALSA, World-Wide Computer Carlos Varela RPI C. Varela.
Concurrency control abstractions (PDCS 9, CPE 5*)
Carlos Varela Rennselaer Polytechnic Institute October 4, 2016
Declarative Computation Model Single assignment store (VRH 2
6.001 SICP Interpretation Parts of an interpreter
Concurrent Programming Actors, SALSA, Coordination Abstractions
Corresponds with Chapter 5
Presentation transcript:

C. Varela1 Actors in SALSA and Erlang (PDCS 9, CPE 5*) Support for actor model in SALSA and Erlang Carlos Varela Rennselaer Polytechnic Institute October 9, 2015 * Concurrent Programming in Erlang, by J. Armstrong, R. Virding, C. Wikström, M. Williams

Agha, Mason, Smith & Talcott 1.Extend a functional language (λ-calculus + ifs and pairs) with actor primitives. 2.Define an operational semantics for actor configurations. 3.Study various notions of equivalence of actor expressions and configurations. 4.Assume fairness: –Guaranteed message delivery. –Individual actor progress. C. Varela2

λ-Calculus as a Model for Sequential Computation Syntax e ::= vvalue | λv.efunctional abstraction | ( e e )application Example of beta-reduction: ( λx.x 2 2 ) x 2 {2/x} C. Varela3 λx.x 2 2 2

Actor Primitives send(a,v) –Sends value v to actor a. new(b) –Creates a new actor with behavior b (a λ-calculus abstraction) and returns the identity/name of the newly created actor. ready(b) –Becomes ready to receive a new message with behavior b. C. Varela4

AMST Actor Language Examples b5 = rec(λy. λx.seq(send(x,5),ready(y))) receives an actor name x and sends the number 5 to that actor, then it becomes ready to process new messages with the same behavior y. Sample usage: send(new(b5), a) A sink, an actor that disregards all messages: sink = rec(λb. λm.ready(b)) C. Varela5

Operational Semantics for AMST Actor Language Operational semantics of actor model as a labeled transition relationship between actor configurations: [label] k 1 k 2 Actor configurations model open system components: –Set of individually named actors –Messages “en-route” C. Varela6

Actor Configurations k = α || µ α is a function mapping actor names (represented as free variables) to actor states. µ is a multi-set of messages “en-route.” C. Varela7

Reduction contexts and redexes Consider the expression: e = send(new(b5), a) The redex r represents the next sub-expression to evaluate in a left-first call-by-value evaluation strategy. The reduction context R (or continuation) is represented as the surrounding expression with a hole replacing the redex. send(new(b5), a) = send( ☐,a)  new(b5) e = R r where R = send( ☐,a) r = new(b5) C. Varela8

Operational Semantics of Actors C. Varela9

Operational semantics example (1) k 0 = [send( ☐,a)  new(b5) ] a || {} k 1 = [send(b,a)] a, [ready(b5)] b || {} [new:a,b] k 0 k 1 k 2 = [nil] a, [ready(b5)] b || { } [snd:a] k 1 k 2 C. Varela10

Operational semantics example (2) k 2 = [nil] a, [ready(b5)] b || { } k 3 = [nil] a, [rec(λy.λx.seq(send(x,5),ready(y)))(a)] b || {} [rcv:b,a] k 2 k 3 k 4 = [nil] a, [seq(send(a,5),ready(b5)))] b || {} [fun:b] k 3 k 4 C. Varela11

Operational semantics example (3) k 4 = [nil] a, [seq( ☐,ready(b5)) send(a,5) ] b || {} [snd:a,5] k 4 k 5 k 5 = [nil] a, [seq(nil,ready(b5))] b || { } C. Varela12

Operational semantics example (4) k 5 = [nil] a, [seq(nil,ready(b5))] b || { } k 6 = [nil] a, [ready(b5)] b || { } [fun:b] k 5 k 6 C. Varela13

Semantics example summary k 0 = [send(new(b5),a)] a || {} k 6 = [nil] a, [ready(b5)] b || { } [new:a,b] [snd:a] [rcv:b,a] [fun:b] k 0 k 1 k 2 k 3 k 4 [snd:a,5] [fun:b] k 4 k 5 k 6 C. Varela14 This sequence of (labeled) transitions from k 0 to k 6 is called a computation sequence.

Reference Cell cell = rec(λb.λc.λm. if ( get?(m), seq( send(cust(m), c), ready(b(c))) if ( set?(m), ready(b(contents(m))), ready(b(c))))) Using the cell: let a = new(cell(0)) in seq( send(a, mkset(7)), send(a, mkset(2)), send(a, mkget(c))) C. Varela15

Asynchronous communication k 0 = [ready(cell(0))] a || {,, } Three receive transitions are enabled at k 0. [rcv:a,s(7)] k 0 k 1 [rcv:a,s(2)] k 0 k 1 ’ [rcv:a,g(c)] k 0 k 1 ” C. Varela16 Multiple enabled transitions can lead to nondeterministic behavior The set of all computations sequences from k 0 is called the computation tree τ(k 0 ).

Nondeterministic behavior (1) k 0 = [ready(cell(0))] a || {,, } k 1 * [ready(cell(7))] a || {, } k 1 ’ * [ready(cell(2))] a || {, } k 1 ” * [ready(cell(0))] a || {,, } C. Varela17 Customer c will get 2 or 7. Customer c will get 0.

Nondeterministic behavior (2) k 0 = [ready(cell(0))] a || {,, } Order of three receive transitions determines final state, e.g.: [rcv:a,g(c)] [rcv:a,s(7)] [rcv:a,s(2)] k 0 k 1 * k 2 * k 3 k f = [ready(cell(2))] a || { } C. Varela18 Final cell state is 2.

Nondeterministic behavior (3) k 0 = [ready(cell(0))] a || {,, } Order of three receive transitions determines final state, e.g.: [rcv:a,s(2)] [rcv:a,g(c)] [rcv:a,s(7)] k 0 k 1 * k 2 * k 3 k f = [ready(cell(7))] a || { } C. Varela19 Final cell state is 7.

C. Varela20 Actors/SALSA Actor Model –A reasoning framework to model concurrent computations –Programming abstractions for distributed open systems G. Agha, Actors: A Model of Concurrent Computation in Distributed Systems. MIT Press, Agha, Mason, Smith and Talcott, “A Foundation for Actor Computation”, J. of Functional Programming, 7, 1-72, SALSA –Simple Actor Language System and Architecture –An actor-oriented language for mobile and internet computing –Programming abstractions for internet-based concurrency, distribution, mobility, and coordination C. Varela and G. Agha, “Programming dynamically reconfigurable open systems with SALSA”, ACM SIGPLAN Notices, OOPSLA 2001, 36(12), pp

C. Varela21 SALSA support for Actors Programmers define behaviors for actors. Actors are instances of behaviors. Messages are modeled as potential method invocations. Messages are sent asynchronously. State is modeled as encapsulated objects/primitive types. Tokens represent future message return values. Continuation primitives are used for coordination.

C. Varela22 Reference Cell Example module cell; behavior Cell { Object content; Cell(Object initialContent) { content = initialContent; } Object get() { return content; } void set(Object newContent) { content = newContent; }

C. Varela23 Reference Cell Example module cell; behavior Cell { Object content; Cell(Object initialContent) { content = initialContent; } Object get() { return content; } void set(Object newContent) { content = newContent; } Encapsulated state content. Actor constructor. Message handlers. State change.

C. Varela24 Reference Cell Example module cell; behavior Cell { Object content; Cell(Object initialContent) { content = initialContent; } Object get() { return content; } void set(Object newContent) { content = newContent; } return asynchronously sets token associated to get message. Implicit control loop: End of message implies ready to receive next message.

C. Varela25 Cell Tester Example module cell; behavior CellTester { void act( String[] args ) { Cell c = new Cell(0); c <- set(2); c <- set(7); token t = c <- get(); standardOutput <- println( t ); }

C. Varela26 Cell Tester Example module cell; behavior CellTester { void act( String[] args ) { Cell c = new Cell(0); c <- set(2); c <- set(7); token t = c <- get(); standardOutput <- println( t ); } Actor creation (new) Message passing ( <- ) println message can only be processed when token t from c’s get() message handler has been produced.

C. Varela27 Cell Tester Example module cell; behavior CellTester { void act( String[] args ) { Cell c = new Cell(0); c <- set(2); c <- set(7); token t = c <- get(); standardOutput <- println( t ); } All message passing is asynchronous. println message is called partial until token t is produced. Only full messages (with no pending tokens) are delivered to actors.

C. Varela28 SALSA compiles to Java SALSA source files are compiled into Java source files before being compiled into Java byte code. SALSA programs may take full advantage of the Java API.

C. Varela29 Erlang support for Actors Actors in Erlang are modeled as processes. Processes start by executing an arbitrary function. Related functions are grouped into modules. Messages can be any Erlang terms, e.g., atoms, tuples (fixed arity), or lists (variable arity). Messages are sent asynchronously. State is modeled implicitly with function arguments. Actors explicitly call receive to get a message, and must use tail-recursion to get new messages, i.e., control loop is explicit.

Reference Cell in Erlang -module(cell). -export([cell/1]). cell(Content) -> receive {set, NewContent} -> cell(NewContent); {get, Customer} -> Customer ! Content, cell(Content) end. C. Varela30

Reference Cell in Erlang -module(cell). -export([cell/1]). cell(Content) -> receive {set, NewContent} -> cell(NewContent); {get, Customer} -> Customer ! Content, cell(Content) end. C. Varela31 Encapsulated state Content. Messag e handlers State change. Explicit control loop: Actions at the end of a message need to include tail-recursive function call. Otherwise actor (process) terminates.

Reference Cell in Erlang -module(cell). -export([cell/1]). cell(Content) -> receive {set, NewContent} -> cell(NewContent); {get, Customer} -> Customer ! Content, cell(Content) end. C. Varela32 Content is an argument to the cell function. {set, NewContent} is a tuple pattern. set is an atom. NewContent is a variable. Messages are checked one by one, and for each message, first pattern that applies gets its actions (after -> ) executed. If no pattern matches, messages remain in actor’s mailbox.

Cell Tester in Erlang -module(cellTester). -export([main/0]). main() -> C = spawn(cell,cell,[0]), C!{set,2}, C!{set,7}, C!{get,self()}, receive Value -> io:format("~w~n”,[Value]) end. C. Varela33

Cell Tester in Erlang -module(cellTester). -export([main/0]). main() -> C = spawn(cell,cell,[0]), C!{set,2}, C!{set,7}, C!{get,self()}, receive Value -> io:format("~w~n”,[Value]) end. C. Varela34 Actor creation (spawn) Message passing ( ! ) receive waits until a message is available.

Cell Tester in Erlang -module(cellTester). -export([main/0]). main() -> C = spawn(cell,cell,[0]), C!{set,2}, C!{set,7}, C!{get,self()}, receive Value -> io:format("~w~n",[Value]) end. C. Varela35 [0] is a list with the arguments to the module’s function. General form: spawn(module, function, arguments) [0] is a list with the arguments to the module’s function. General form: spawn(module, function, arguments) Function calls take the form: module:function(args) self() is a built-in function (BIF) that returns the process id of the current process.

Tree Product Behavior in AMST B treeprod = rec(λb.λm. seq(if(isnat(tree(m)), send(cust(m),tree(m)), let newcust=new(B joincont (cust(m))), lp = new(B treeprod ), rp = new(B treeprod ) in seq(send(lp, pr(left(tree(m)),newcust)), send(rp, pr(right(tree(m)),newcust)))), ready(b))) C. Varela36

Join Continuation in AMST B joincont = λcust.λfirstnum.ready(λnum. seq(send(cust,firstnum*num), ready(sink))) C. Varela37

Sample Execution C. Varela38 cust f(tree,cust) JC cust JC (a)(b) f(left(tree),JC)f(right(tree),JC)

Sample Execution C. Varela39 cust JC ’ JC cust JC firstnum (c) JC ' JC firstnum JC ' cust firstnum JC (d) JC ’ f(left(tree),JC)

Sample Execution C. Varela40 num Cust firstnum Cust JC (e) firstnum * num Cust (f)

Tree Product Behavior in Erlang -module(treeprod). -export([treeprod/0,join/1]). treeprod() -> receive {{Left, Right}, Customer} -> NewCust = spawn(treeprod,join,[Customer]), LP = spawn(treeprod,treeprod,[]), RP = spawn(treeprod,treeprod,[]), LP!{Left,NewCust}, RP!{Right,NewCust}; {Number, Customer} -> Customer ! Number end, treeprod(). join(Customer) -> receive V1 -> receive V2 -> Customer ! V1*V2 end end. C. Varela41

Tree Product Sample Execution 2> TP = spawn(treeprod,treeprod,[]). 3> TP ! {{{{5,6},2},{3,4}},self()}. {{{{5,6},2},{3,4}}, } 4> flush(). Shell got 720 ok 5> C. Varela42

C. Varela43 Tree Product Behavior in SALSA module treeprod; behavior TreeProduct { void compute(Tree t, UniversalActor c){ if (t.isLeaf()) c <- result(t.value()); else { JoinCont newCust = new JoinCont(c); TreeProduct lp = new TreeProduct(); TreeProduct rp = new TreeProduct(); lp <- compute(t.left(), newCust); rp <- compute(t.right(), newCust); }

C. Varela44 Join Continuation in SALSA module treeprod; behavior JoinCont { UniversalActor cust; int first; boolean receivedFirst; JoinCont(UniversalActor cust){ this.cust = cust; this.receivedFirst = false; } void result(int v) { if (!receivedFirst){ first = v; receivedFirst = true; } else // receiving second value cust <- result(first*v); }

C. Varela45 Summary Actors are concurrent entities that react to messages. –State is completely encapsulated. There is no shared memory! –Message passing is asynchronous. –Actor run-time has to ensure fairness. AMST extends the call by value lambda calculus with actor primitives. State is modeled as function arguments. Actors use ready to receive new messages. Erlang extends a functional programming language core with processes that run arbitrary functions. State is implicit in the function’s arguments. Control loop is explicit: actors use receive to get a message, and tail-form recursive call to continue. SALSA extends an object-oriented programming language (Java) with universal actors. State is encapsulated in instance variables. Control loop is implicit: ending a message handler, signals readiness to receive a new message.

Exercises 41.Define pairing primitives ( pr, 1st, 2nd ) in the pure lambda calculus. 42.PDCS Exercise (page 77). 43.Modify the treeprod behavior in Erlang to reuse the tree productor actor to compute the product of the left subtree. (See PDCS page 63 for the corresponding tprod 2 behavior in AMST.) 44.PDCS Exercise (page 203). 45.Create a concurrent fibonacci behavior in Erlang using join continuations. C. Varela46