SlideShare a Scribd company logo
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

More Related Content

PDF
Elm talk bayhac2015
PDF
Temporal logic and functional reactive programming
PDF
"That scripting language called Prolog"
PPTX
Functional programming
PDF
Introducing: A Complete Algebra of Data
PDF
Haskell for data science
PPTX
Systematic Generation Data and Types in C++
PPTX
ScalaDays 2013 Keynote Speech by Martin Odersky
Elm talk bayhac2015
Temporal logic and functional reactive programming
"That scripting language called Prolog"
Functional programming
Introducing: A Complete Algebra of Data
Haskell for data science
Systematic Generation Data and Types in C++
ScalaDays 2013 Keynote Speech by Martin Odersky

What's hot (20)

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

Similar to Introduction to join calculus (20)

PDF
High-Performance Haskell
PPT
CollectionConsiderationsCollectionConsiderations.ppt
PDF
Fp in scala part 2
ODP
Scala as a Declarative Language
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
PDF
On the Expressiveness of Attribute-based Communication
PDF
Coding in Style
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
PPT
Rewriting Java In Scala
PDF
Monadologie
PPTX
RxJava 2 Reactive extensions for the JVM
PDF
Introduction to Functional Programming with Scala
PDF
Parametricity - #cljsyd - May, 2015
PPTX
Intro to Reactive Thinking and RxJava 2
PDF
Integral Calculus Anti Derivatives reviewer
ODP
Concurrency on the JVM
PDF
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
PPTX
Principles of functional progrmming in scala
PPT
Jeop game-final-review
PDF
The Scala Programming Language
High-Performance Haskell
CollectionConsiderationsCollectionConsiderations.ppt
Fp in scala part 2
Scala as a Declarative Language
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
On the Expressiveness of Attribute-based Communication
Coding in Style
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Rewriting Java In Scala
Monadologie
RxJava 2 Reactive extensions for the JVM
Introduction to Functional Programming with Scala
Parametricity - #cljsyd - May, 2015
Intro to Reactive Thinking and RxJava 2
Integral Calculus Anti Derivatives reviewer
Concurrency on the JVM
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Principles of functional progrmming in scala
Jeop game-final-review
The Scala Programming Language
Ad

Recently uploaded (20)

PPT
Animal tissues, epithelial, muscle, connective, nervous tissue
PDF
lecture 2026 of Sjogren's syndrome l .pdf
PDF
Unit 5 Preparations, Reactions, Properties and Isomersim of Organic Compounds...
PPTX
INTRODUCTION TO PAEDIATRICS AND PAEDIATRIC HISTORY TAKING-1.pptx
PPTX
PMR- PPT.pptx for students and doctors tt
PDF
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
PDF
S2 SOIL BY TR. OKION.pdf based on the new lower secondary curriculum
PPTX
POULTRY PRODUCTION AND MANAGEMENTNNN.pptx
PPTX
ap-psych-ch-1-introduction-to-psychology-presentation.pptx
PPTX
BODY FLUIDS AND CIRCULATION class 11 .pptx
PPTX
Fluid dynamics vivavoce presentation of prakash
PDF
Looking into the jet cone of the neutrino-associated very high-energy blazar ...
PPTX
Introcution to Microbes Burton's Biology for the Health
PPTX
BIOMOLECULES PPT........................
PPT
1. INTRODUCTION TO EPIDEMIOLOGY.pptx for community medicine
PPT
veterinary parasitology ````````````.ppt
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PPTX
gene cloning powerpoint for general biology 2
PPTX
Substance Disorders- part different drugs change body
PPTX
Hypertension_Training_materials_English_2024[1] (1).pptx
Animal tissues, epithelial, muscle, connective, nervous tissue
lecture 2026 of Sjogren's syndrome l .pdf
Unit 5 Preparations, Reactions, Properties and Isomersim of Organic Compounds...
INTRODUCTION TO PAEDIATRICS AND PAEDIATRIC HISTORY TAKING-1.pptx
PMR- PPT.pptx for students and doctors tt
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
S2 SOIL BY TR. OKION.pdf based on the new lower secondary curriculum
POULTRY PRODUCTION AND MANAGEMENTNNN.pptx
ap-psych-ch-1-introduction-to-psychology-presentation.pptx
BODY FLUIDS AND CIRCULATION class 11 .pptx
Fluid dynamics vivavoce presentation of prakash
Looking into the jet cone of the neutrino-associated very high-energy blazar ...
Introcution to Microbes Burton's Biology for the Health
BIOMOLECULES PPT........................
1. INTRODUCTION TO EPIDEMIOLOGY.pptx for community medicine
veterinary parasitology ````````````.ppt
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
gene cloning powerpoint for general biology 2
Substance Disorders- part different drugs change body
Hypertension_Training_materials_English_2024[1] (1).pptx

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