ICLP@Unibuc c6-actors
ICLP@Unibuc c6-actors
programare
Actor-based programming — Erlang/Elixir
FMI @ UNIBUC
1
Shared memory model limitations
Actors
Erlang / Elixir
2
Shared memory model limitations
The problem with objects and threads (shared memory)
Ilusion of encapsulation
No-win situation
4
The problem with locks and synchronization mechanisms
No-win situation
4
The problem with locks and synchronization mechanisms
No-win situation
4
The problem with locks and synchronization mechanisms
No-win situation
4
The problem with locks and synchronization mechanisms
No-win situation
4
The problem with locks and synchronization mechanisms
4
The problem with locks and synchronization mechanisms
4
The problem with locks and synchronization mechanisms
4
The problem with locks and synchronization mechanisms
4
Ilusion of shared memory
5
Ilusion of shared memory
5
Ilusion of shared memory
5
Ilusion of shared memory
5
Ilusion of a call stack
7
The actor model
7
The actor model
7
The actor model
7
The actor model
7
The actor model
7
Everybody is an actor!
8
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
Defining characteristics
9
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
• create new actors;
Defining characteristics
9
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
• create new actors;
• designate the behavior to be used for the next message it
receives.
Defining characteristics
9
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
• create new actors;
• designate the behavior to be used for the next message it
receives.
Defining characteristics
• Interaction only through asynchronous communication, using
message passing
9
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
• create new actors;
• designate the behavior to be used for the next message it
receives.
Defining characteristics
• Interaction only through asynchronous communication, using
message passing
• Inherent concurrency of computation within and among actors
9
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
• create new actors;
• designate the behavior to be used for the next message it
receives.
Defining characteristics
• Interaction only through asynchronous communication, using
message passing
• Inherent concurrency of computation within and among actors
• Private communication based on address
9
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
• create new actors;
• designate the behavior to be used for the next message it
receives.
Defining characteristics
• Interaction only through asynchronous communication, using
message passing
• Inherent concurrency of computation within and among actors
• Private communication based on address
• Agent can only communicate with
9
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
• create new actors;
• designate the behavior to be used for the next message it
receives.
Defining characteristics
• Interaction only through asynchronous communication, using
message passing
• Inherent concurrency of computation within and among actors
• Private communication based on address
• Agent can only communicate with
• The agents it created (its children)
9
Actors
Definition
An actor is a computational entity that, in response to a message it
receives, can concurrently:
• send a messages to other actors;
• create new actors;
• designate the behavior to be used for the next message it
receives.
Defining characteristics
• Interaction only through asynchronous communication, using
message passing
• Inherent concurrency of computation within and among actors
• Private communication based on address
• Agent can only communicate with
• The agents it created (its children)
9
• Agents of whom it found out through messages it received
When an actor receives a message:
10
When an actor receives a message:
10
When an actor receives a message:
10
Problems solved by actors
11
Problems solved by actors
11
Problems solved by actors
11
Problems solved by actors
11
Problems solved by actors
11
Erlang / Elixir
1
Erlang
12
1
Erlang
12
1
Erlang
12
1
Erlang
12
1
Erlang
12
1
Erlang
12
1
Erlang
12
1
Erlang
• Functional language
13
Erlang language features
• Functional language
• no mutable state
13
Erlang language features
• Functional language
• no mutable state
• higher-order functions
13
Erlang language features
• Functional language
• no mutable state
• higher-order functions
• dynamically typed
13
Erlang language features
• Functional language
• no mutable state
• higher-order functions
• dynamically typed
• Compiles to bytecode, runs on a VM called BEAM
Open Telecom Platform (OTP)
13
Erlang language features
• Functional language
• no mutable state
• higher-order functions
• dynamically typed
• Compiles to bytecode, runs on a VM called BEAM
Open Telecom Platform (OTP)
• collection of middleware, libraries and tools in Erlang
13
Erlang language features
• Functional language
• no mutable state
• higher-order functions
• dynamically typed
• Compiles to bytecode, runs on a VM called BEAM
Open Telecom Platform (OTP)
• collection of middleware, libraries and tools in Erlang
• Supporting development in Erlang
13
Erlang language features
• Functional language
• no mutable state
• higher-order functions
• dynamically typed
• Compiles to bytecode, runs on a VM called BEAM
Open Telecom Platform (OTP)
• collection of middleware, libraries and tools in Erlang
• Supporting development in Erlang
• APIs for building robust distributed systems
13
Elixir
Goals
14
Elixir
14
Elixir
14
Elixir
14
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir improvements over Erlang
15
Elixir resources
16
Elixir resources
16
Elixir resources
16
Elixir resources
16
Elixir resources
16
Elixir resources
16
Elixir resources
16
Elixir resources
16
Getting started
Scripts
• files with extension .exs
17
Getting started
Scripts
• files with extension .exs
• can be loaded into the iex interpreter
17
Getting started
Scripts
• files with extension .exs
• can be loaded into the iex interpreter
• can be executed using elixir
17
Datatypes (numbers)
iex> is_integer(1)
true
iex> is_integer 0x1F
true
iex> is_integer ?s,
true
iex> is_number 1
true
iex> is_float 1.0
true
iex> is_number 3.14
true
18
Datatypes (booleans and atoms)
Lists of characters
iex> [104, 101, 108, 108, 111]
22
'hello'
Lists
Lists of characters
iex> [104, 101, 108, 108, 111]
22
'hello'
Boolean operators
iex(45)> ! nil
true
iex(46)> nil && false
nil
iex(47)> nil || false
false 23
Boolean operators
iex> 1 == 1
true
iex> 1 != 2
true
iex> 1 < 2
true
iex> 1 == 1.0
true
iex> 1 === 1.0
false
iex> 1 < :atom
true
• number < atom < reference < function < port < pid < tuple
< map < list < bitstring 24
Higher-order functions
26
Pattern matching basics
• Match on structure
• Match on structure
Pinning operator ˆ
iex> x = 1
1
iex> ˆx = 2 28
case with matching clauses and guards
29
(Anonymous) functions with clauses and guards
iex> f = fn
...> x, y when x > 0 -> x + y
...> x, y -> x * y
...> end
#Function<12.71889879/2 in :erl_eval.expr/5>
iex> f.(1, 3)
4
iex> f.(-1, 3)
-3
30