SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Introduction to Join Calculus
Sergei Winitzki
Scala Study Group
November 10, 2013
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 1 / 15
Motivation
Imperative concurrency is dicult:
callbacks, semaphores, locks, threads, shared state
testing??
Pure functional concurrency is better:
futures = async monads
Erlang's purely functional messaging; actors (Akka in Scala)
Join Calculus:
Elegant, concise model of concurrent computation
Join Calculus = more purely functionally concurrent actors
Working implementation: JoCaml (jocaml.inria.fr)
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 2 / 15
A taste of OCaml
Common features to F#, Haskell, OCaml, SML, Scala:
Expression-oriented programming:
let s = (if 1=1 then Hello, world! else Error) in print_string s
Algebraic data types, parametric polymorphism:
type 'a bTree = Leaf of 'a | Node of ('a bTree * 'a bTree)
Immutable, scoped values, with statically inferred types:
# let x = 3 in (let x = x+1 in x/2) * x;;
- : int = 6
Mutually recursive denitions:
# let rec isEven n = if n=0 then true else isOdd (n-1)
and isOdd n = if n=0 then false else isEven (n-1);;
val isEven : int - bool = fun
val isOdd : int - bool = fun
# let result = List.map (fun x - (isEven x, isOdd x)) [1; 2];;
val result : (bool * bool) list = [ (false, true); (true, false) ]
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 3 / 15
Join Calculus in a nutshell
The Reexive Chemical Abstract Machine (RChAM)
Abstract chemistry: molecules and reactions
Chemical soup contains many molecules
A group of molecules starts a chemical reaction
jocaml def a()  b() = c()
and a()  c() = 0;;
val a : unit Join.chan = abstr
val b : unit Join.chan = abstr
val c : unit Join.chan = abstr
A
B
C
C
A
Using the chemical machine:
We dene arbitrary chemical laws and molecules: a, b, c, ...
We inject some molecules into the soup: spawn a()  a()  b()
Note: a()  a()  b() is the syntax for molecule-valued expressions
The runtime system evolves the soup asynchronously
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 4 / 15
Join Calculus in a nutshell
Concurrent computations
Sequential computation = evaluating an expression
Concurrent computation = evaluating several expressions at once
Join Calculus organizes concurrent computations through chemistry:
Each molecule carries a value
Each reaction computes a
molecule-valued expression
Results of computation are
injected back into the soup
Reactions start asynchronously
after injecting initial molecules
a(x)
b(y) c()
a(x)
print x
a(z)compute z(x,y)
def a(x)  b(y) =
let z = (big_function x y) in a(z)
and a(x)  c() = (print_int x; 0);;
When reaction starts: input molecules disappear, expression is computed,
output molecules are injected
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 5 / 15
Join Calculus in a nutshell
More features of JoCaml
Mutual recursion:
def a(x) = a(x+1)  b(x+2) and b(x)  c(y) = a(x+y)
Pattern-matching on molecule's payload values:
def a(Some x)  b(y) = b(x+y) or a(None)  b(y) = b(y)
Instant molecules (same type as function calls):
def a(x)  f() = a(x+1)  reply x to f
Local molecules and reactions:
def c(n) = ( if n0 then c(n-1) else 0 ) in spawn c(10)
Injection as side-eect: let x=3 in (spawn a(x); printf %dn x)
Molecule constructors are dened as values and can be manipulated:
# def a(x) = Printf.printf %dn x; 0;;
val a : int Join.chan = abstr
# def b(m,y) = Printf.printf injecting m(%d)n y; m(y);;
val b: (int Join.chan * int) Join.chan = abstr
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 6 / 15
Join Calculus: Examples
Options, Futures, and Map/Reduce
Future with synchronous poll (get):
# def fut(f,x) = let res = f x in finished(res)
and get()  finished(res) = reply res to get;;
val get : unit - '_a = fun
val finished : '_a Join.chan = abstr
val fut : (('a - '_b) * 'a) Join.chan = abstr
Future with synchronous callback:
def fut(f,x,c) = let res = f x in ( c(res); finished(res) )
and get()  finished(res) = reply res to get
Future with asynchronous callback:
def fut(f,x,m) = let res = f x in ( m(res)  finished(res) )
and get()  finished(res) = reply res to get
Exercise: implement a future with cancellable callback
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 7 / 15
Join Calculus: Examples
Options, Futures, and Map/Reduce
Asynchronous counter:
# def inc()  c(n) = c(n+1)
or get()  c(n) = reply n to get  c(n);;
val inc : unit Join.chan = abstr
val get : unit - int = fun
val c : int Join.chan = abstr
# spawn c(0)  inc()  inc()  inc();;
- : unit = ()
# get();;
- : int = 3
Map/Reduce:
def res(list)  c(s) = res (s::list) or get()  res(list) = reply list to get;;
spawn res([]);;
List.map (fun x- spawn c(x*2)) [1; 2; 3];;
get();; (* this returned [4; 6; 2] in one test *)
Exercise: implement a concurrent fold (e.g. sum of int list)
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 8 / 15
Join Calculus: Examples
Five Dining Philosophers
Philosophers A, B, C, D, E; forks fAB, fBC, fCD, fDE, fEA.
let report(message) = Printf.printf %sn message;
Unix.sleep (Random.int 3600) ;;
def hA()  fEA()  fAB() = report(A is eating); tA()  fEA()  fAB()
or hB()  fAB()  fBC() = report(B is eating); tB()  fAB()  fBC()
or hC()  fBC()  fCD() = report(C is eating); tC()  fBC()  fCD()
or hD()  fCD()  fDE() = report(D is eating); tD()  fCD()  fDE()
or hE()  fDE()  fEA() = report(E is eating); tE()  fDE()  fEA()
and tA() = report(A is thinking); hA()
and tB() = report(B is thinking); hB()
and tC() = report(C is thinking); hC()
and tD() = report(D is thinking); hD()
and tE() = report(E is thinking); hE() ;;
spawn fAB()  fBC()  fCD()  fDE()  fEA()
 tA()  tB()  tC()  tD()  tE() ;;
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 9 / 15
Limitations and restrictions of Join Calculus
Less is more!
Reactions are dened statically and with local scope:
no molecules with computed names:
a(x)  molecule_named(b)(x) = (not allowed!)
cannot dynamically add a new reaction to a previously dened
molecule:
def a(x)  b(y) = ... ;;
def b(y)  c(z) = ... shadows the old definition of b()!
No guard conditions for reactions:
def a(x)  b(y)  start_if (x==y) = ... (not allowed!)
No duplicated input values: a(x)  b(x) = (not allowed!)
No duplicated input molecules: a(x)  a(y) = (not allowed!)
No way to test dynamically for the presence/absence of a molecule!
def a(x)  b(y) = if have_molecules(c  d) then ... else ... (not allowed!)
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 10 / 15
Limitations and restrictions of Join Calculus
It seems they do not limit the expressive power!
What if we need a reaction with pairs of molecules?
a(x)  a(y) = a(x+y)
Solution: use two or-coupled reactions with new molecules a' and b:
def a(x)  b() = a'(x) or a(x)  a'(y) = whatever(x,y)
Make sure that one b() is injected together with each a(x)
Questions:
Can we prevent the error of not injecting b()?
Can we do a reaction with n molecules, where n is dynamic?
Can we do n dining philosophers?
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 11 / 15
Local scope and recursion
Skeleton code for concurrent merge-sort
The mergesort molecule:
receives the upper-level sorted_result molecule
denes its own sorted molecule in local scope
emits upper-level sorted_result when done
def mergesort(arr, sorted_result) =
if Array.length arr = 1 then sorted_result(arr)
else
let (part1, part2) = array_split arr
in
def sorted(x)  sorted(y) = sorted_result(array_merge x y)
in
mergesort(part1, sorted)  mergesort(part2, sorted)
Note: sorted(x)  sorted(y) is pseudocode, easy to rewrite.
See tutorial for complete working JoCaml code.
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 12 / 15
Comparison: Join Calculus vs. Actor model
Reaction ≈ actor; molecule ≈ message to actor.
Actors:
receive messages asynchronously
process one message at a time (one actor = one thread)
must hold mutable state (e.g. for a thread-safe counter)
explicitly create and congure other actors
Reactions:
start when several molecules are available
many reactions can start at once, automatically
do not need mutable state
all reactions are dened statically, but locally scoped
simulate actors: def message(m)  actor(state) = actor(compute_new state m)
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 13 / 15
Implementation of Join Calculus
JC = a DSL + run-time library, or just DSL?
Implement Join Calculus using Actors (Akka)?
Each reaction has 1 monitor actor and ≥ 1 worker actors
Monitor receives messages for each spawn, keeps track of molecules
Monitor starts a worker actor when all molecules are present
Monitors have to talk to competing monitors - use up molecules
but all competitions are statically dened!
Monitors / workers need to be locally scoped!
No globally shared state of the soup is needed!
Discuss
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 14 / 15
Conclusions and outlook
Join Calculus is concurrent programming in pure functional style
Similar to Actors, but more concurrent and more pure
Very little known, and very little used in practice
Existing literature is not suitable as introduction to practical
programming
My tutorial text is in progress (search Google for tutorial on join
calculus)
Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 15 / 15

Contenu connexe

Tendances

Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Piotr Paradziński
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about lazinessJohan Tibell
 
Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Sumant Tambe
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scalaPiotr Paradziński
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Sumant Tambe
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayKevlin Henney
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
Scope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesScope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesEelco Visser
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойSigma Software
 
Declarative Language Definition
Declarative Language DefinitionDeclarative Language Definition
Declarative Language DefinitionEelco Visser
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScripttmont
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocamlpramode_ce
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Brendan Eich
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingFilip De Sutter
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressBrendan Eich
 

Tendances (20)

Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
Introduction to Julia Language
Introduction to Julia LanguageIntroduction to Julia Language
Introduction to Julia Language
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went Away
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Scope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesScope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languages
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
 
Declarative Language Definition
Declarative Language DefinitionDeclarative Language Definition
Declarative Language Definition
 
Fluent14
Fluent14Fluent14
Fluent14
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progress
 

Similaire à Introduction to join calculus

High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
 
On the Expressiveness of Attribute-based Communication
On the Expressiveness of Attribute-based CommunicationOn the Expressiveness of Attribute-based Communication
On the Expressiveness of Attribute-based CommunicationYehia ABD ALRahman
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Philip Schwarz
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In ScalaSkills Matter
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVMNetesh Kumar
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
Parametricity - #cljsyd - May, 2015
Parametricity - #cljsyd - May, 2015Parametricity - #cljsyd - May, 2015
Parametricity - #cljsyd - May, 2015Leonardo Borges
 
Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2JollyRogers5
 
Integral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerIntegral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerJoshuaAgcopra
 
Concurrency on the JVM
Concurrency on the JVMConcurrency on the JVM
Concurrency on the JVMVaclav Pech
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Peng Cheng
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 

Similaire à Introduction to join calculus (20)

High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
 
On the Expressiveness of Attribute-based Communication
On the Expressiveness of Attribute-based CommunicationOn the Expressiveness of Attribute-based Communication
On the Expressiveness of Attribute-based Communication
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 
Monadologie
MonadologieMonadologie
Monadologie
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Parametricity - #cljsyd - May, 2015
Parametricity - #cljsyd - May, 2015Parametricity - #cljsyd - May, 2015
Parametricity - #cljsyd - May, 2015
 
Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2
 
Integral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerIntegral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewer
 
Concurrency on the JVM
Concurrency on the JVMConcurrency on the JVM
Concurrency on the JVM
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
Jeop game-final-review
Jeop game-final-reviewJeop game-final-review
Jeop game-final-review
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 

Dernier

Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )aarthirajkumar25
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000Sapana Sha
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticssakshisoni2385
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡anilsa9823
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
fundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyfundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyDrAnita Sharma
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfSumit Kumar yadav
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoSérgio Sacani
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfSumit Kumar yadav
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPirithiRaju
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxUmerFayaz5
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINsankalpkumarsahoo174
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptxRajatChauhan518211
 
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdfPests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdfPirithiRaju
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)PraveenaKalaiselvan1
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRDelhi Call girls
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)Areesha Ahmad
 

Dernier (20)

Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
fundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyfundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomology
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdf
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdf
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdf
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptx
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdfPests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)
 

Introduction to join calculus

  • 1. Introduction to Join Calculus Sergei Winitzki Scala Study Group November 10, 2013 Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 1 / 15
  • 2. Motivation Imperative concurrency is dicult: callbacks, semaphores, locks, threads, shared state testing?? Pure functional concurrency is better: futures = async monads Erlang's purely functional messaging; actors (Akka in Scala) Join Calculus: Elegant, concise model of concurrent computation Join Calculus = more purely functionally concurrent actors Working implementation: JoCaml (jocaml.inria.fr) Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 2 / 15
  • 3. A taste of OCaml Common features to F#, Haskell, OCaml, SML, Scala: Expression-oriented programming: let s = (if 1=1 then Hello, world! else Error) in print_string s Algebraic data types, parametric polymorphism: type 'a bTree = Leaf of 'a | Node of ('a bTree * 'a bTree) Immutable, scoped values, with statically inferred types: # let x = 3 in (let x = x+1 in x/2) * x;; - : int = 6 Mutually recursive denitions: # let rec isEven n = if n=0 then true else isOdd (n-1) and isOdd n = if n=0 then false else isEven (n-1);; val isEven : int - bool = fun val isOdd : int - bool = fun # let result = List.map (fun x - (isEven x, isOdd x)) [1; 2];; val result : (bool * bool) list = [ (false, true); (true, false) ] Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 3 / 15
  • 4. Join Calculus in a nutshell The Reexive Chemical Abstract Machine (RChAM) Abstract chemistry: molecules and reactions Chemical soup contains many molecules A group of molecules starts a chemical reaction jocaml def a() b() = c() and a() c() = 0;; val a : unit Join.chan = abstr val b : unit Join.chan = abstr val c : unit Join.chan = abstr A B C C A Using the chemical machine: We dene arbitrary chemical laws and molecules: a, b, c, ... We inject some molecules into the soup: spawn a() a() b() Note: a() a() b() is the syntax for molecule-valued expressions The runtime system evolves the soup asynchronously Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 4 / 15
  • 5. Join Calculus in a nutshell Concurrent computations Sequential computation = evaluating an expression Concurrent computation = evaluating several expressions at once Join Calculus organizes concurrent computations through chemistry: Each molecule carries a value Each reaction computes a molecule-valued expression Results of computation are injected back into the soup Reactions start asynchronously after injecting initial molecules a(x) b(y) c() a(x) print x a(z)compute z(x,y) def a(x) b(y) = let z = (big_function x y) in a(z) and a(x) c() = (print_int x; 0);; When reaction starts: input molecules disappear, expression is computed, output molecules are injected Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 5 / 15
  • 6. Join Calculus in a nutshell More features of JoCaml Mutual recursion: def a(x) = a(x+1) b(x+2) and b(x) c(y) = a(x+y) Pattern-matching on molecule's payload values: def a(Some x) b(y) = b(x+y) or a(None) b(y) = b(y) Instant molecules (same type as function calls): def a(x) f() = a(x+1) reply x to f Local molecules and reactions: def c(n) = ( if n0 then c(n-1) else 0 ) in spawn c(10) Injection as side-eect: let x=3 in (spawn a(x); printf %dn x) Molecule constructors are dened as values and can be manipulated: # def a(x) = Printf.printf %dn x; 0;; val a : int Join.chan = abstr # def b(m,y) = Printf.printf injecting m(%d)n y; m(y);; val b: (int Join.chan * int) Join.chan = abstr Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 6 / 15
  • 7. Join Calculus: Examples Options, Futures, and Map/Reduce Future with synchronous poll (get): # def fut(f,x) = let res = f x in finished(res) and get() finished(res) = reply res to get;; val get : unit - '_a = fun val finished : '_a Join.chan = abstr val fut : (('a - '_b) * 'a) Join.chan = abstr Future with synchronous callback: def fut(f,x,c) = let res = f x in ( c(res); finished(res) ) and get() finished(res) = reply res to get Future with asynchronous callback: def fut(f,x,m) = let res = f x in ( m(res) finished(res) ) and get() finished(res) = reply res to get Exercise: implement a future with cancellable callback Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 7 / 15
  • 8. Join Calculus: Examples Options, Futures, and Map/Reduce Asynchronous counter: # def inc() c(n) = c(n+1) or get() c(n) = reply n to get c(n);; val inc : unit Join.chan = abstr val get : unit - int = fun val c : int Join.chan = abstr # spawn c(0) inc() inc() inc();; - : unit = () # get();; - : int = 3 Map/Reduce: def res(list) c(s) = res (s::list) or get() res(list) = reply list to get;; spawn res([]);; List.map (fun x- spawn c(x*2)) [1; 2; 3];; get();; (* this returned [4; 6; 2] in one test *) Exercise: implement a concurrent fold (e.g. sum of int list) Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 8 / 15
  • 9. Join Calculus: Examples Five Dining Philosophers Philosophers A, B, C, D, E; forks fAB, fBC, fCD, fDE, fEA. let report(message) = Printf.printf %sn message; Unix.sleep (Random.int 3600) ;; def hA() fEA() fAB() = report(A is eating); tA() fEA() fAB() or hB() fAB() fBC() = report(B is eating); tB() fAB() fBC() or hC() fBC() fCD() = report(C is eating); tC() fBC() fCD() or hD() fCD() fDE() = report(D is eating); tD() fCD() fDE() or hE() fDE() fEA() = report(E is eating); tE() fDE() fEA() and tA() = report(A is thinking); hA() and tB() = report(B is thinking); hB() and tC() = report(C is thinking); hC() and tD() = report(D is thinking); hD() and tE() = report(E is thinking); hE() ;; spawn fAB() fBC() fCD() fDE() fEA() tA() tB() tC() tD() tE() ;; Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 9 / 15
  • 10. Limitations and restrictions of Join Calculus Less is more! Reactions are dened statically and with local scope: no molecules with computed names: a(x) molecule_named(b)(x) = (not allowed!) cannot dynamically add a new reaction to a previously dened molecule: def a(x) b(y) = ... ;; def b(y) c(z) = ... shadows the old definition of b()! No guard conditions for reactions: def a(x) b(y) start_if (x==y) = ... (not allowed!) No duplicated input values: a(x) b(x) = (not allowed!) No duplicated input molecules: a(x) a(y) = (not allowed!) No way to test dynamically for the presence/absence of a molecule! def a(x) b(y) = if have_molecules(c d) then ... else ... (not allowed!) Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 10 / 15
  • 11. Limitations and restrictions of Join Calculus It seems they do not limit the expressive power! What if we need a reaction with pairs of molecules? a(x) a(y) = a(x+y) Solution: use two or-coupled reactions with new molecules a' and b: def a(x) b() = a'(x) or a(x) a'(y) = whatever(x,y) Make sure that one b() is injected together with each a(x) Questions: Can we prevent the error of not injecting b()? Can we do a reaction with n molecules, where n is dynamic? Can we do n dining philosophers? Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 11 / 15
  • 12. Local scope and recursion Skeleton code for concurrent merge-sort The mergesort molecule: receives the upper-level sorted_result molecule denes its own sorted molecule in local scope emits upper-level sorted_result when done def mergesort(arr, sorted_result) = if Array.length arr = 1 then sorted_result(arr) else let (part1, part2) = array_split arr in def sorted(x) sorted(y) = sorted_result(array_merge x y) in mergesort(part1, sorted) mergesort(part2, sorted) Note: sorted(x) sorted(y) is pseudocode, easy to rewrite. See tutorial for complete working JoCaml code. Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 12 / 15
  • 13. Comparison: Join Calculus vs. Actor model Reaction ≈ actor; molecule ≈ message to actor. Actors: receive messages asynchronously process one message at a time (one actor = one thread) must hold mutable state (e.g. for a thread-safe counter) explicitly create and congure other actors Reactions: start when several molecules are available many reactions can start at once, automatically do not need mutable state all reactions are dened statically, but locally scoped simulate actors: def message(m) actor(state) = actor(compute_new state m) Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 13 / 15
  • 14. Implementation of Join Calculus JC = a DSL + run-time library, or just DSL? Implement Join Calculus using Actors (Akka)? Each reaction has 1 monitor actor and ≥ 1 worker actors Monitor receives messages for each spawn, keeps track of molecules Monitor starts a worker actor when all molecules are present Monitors have to talk to competing monitors - use up molecules but all competitions are statically dened! Monitors / workers need to be locally scoped! No globally shared state of the soup is needed! Discuss Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 14 / 15
  • 15. Conclusions and outlook Join Calculus is concurrent programming in pure functional style Similar to Actors, but more concurrent and more pure Very little known, and very little used in practice Existing literature is not suitable as introduction to practical programming My tutorial text is in progress (search Google for tutorial on join calculus) Sergei Winitzki (Versal Group Inc.) Introduction to Join Calculus November 10, 2013 15 / 15