erlang - functional programming in a concurrent world · pdf file2 erlang • concurrent...
TRANSCRIPT
![Page 1: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/1.jpg)
1
Erlang - functional programming in a concurrent world
Johan MonteliusKTH
![Page 2: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/2.jpg)
2
Erlang
• Concurrent Oriented Programming– processes have state – communicate using message passing– access and location transparent– asynchronous
• Functional Programming– data structures are immutable
![Page 3: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/3.jpg)
3
History
• Developed at Ericsson in late eighties, early nineties.
• Targeting robust applications in the telecom world.
• Survived despite “everything must be Java”
• Growing interest from outside Ericsson.
![Page 4: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/4.jpg)
4
Today
• Robust server applications:– Amazon SimpleDB - database– GitHub - code repository– RabitMQ - messaging middleware– Facebook - chat
• Why – simple to implement fault tolerance– scales well in distributed systems– multicore performance
![Page 5: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/5.jpg)
5
Erlang
• background• the functional subset• concurrency• distribution• failure detection
![Page 6: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/6.jpg)
6
Data structures
• Literals – atoms: foo, bar, ...– numbers: 123, 1.23, ...– bool: true, false
• Compound– tuples: {foo, 12, {bar, zot}}– lists: [], [foo, 12, bar, zot]
![Page 7: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/7.jpg)
7
Variables
• lexically scoped– implicit scoping– procedure definition– case statement
• untyped– assigned a value when introduced
• Syntax– X, Foo, BarZot, _
![Page 8: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/8.jpg)
8
Assignment and pattern matching
• Assignment of values to variables is done by pattern matching:– <Pattern> = <Expression>
• A pattern can be a single variable:– Foo = 5– Bar = {foo, zot, 42}
• or a compound pattern– {A, B} = {4, 5}– {A, [_, B | T]} = {41, [foo, bar, zot]}
![Page 9: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/9.jpg)
9
Pattern matching can fail
• Pattern matching is also used to do test and to guide the execution:– {person, Name, Number} = {dog, pluto}
![Page 10: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/10.jpg)
10
No circular structures :-(
• You can not construct circular data structures in Erlang.
• Pros– makes the implementation easier
• Cons– I like circular structures.
![Page 11: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/11.jpg)
11
Function definitions
area(X,Y) ->X*Y.
![Page 12: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/12.jpg)
12
If statement
fac(N) ->if
N == 0 -> 1;N > 0 -> N*fac(N-1)
end.
![Page 13: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/13.jpg)
13
Case statement
sum(L) ->case L of
[] ->0;
[H|T] -> H + sum(T)
end.
![Page 14: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/14.jpg)
14
Pattern matching
member(X,L) ->case L of
[] ->no;
[X|_] -> yes;
[_|T] ->member(X, T)
end.
![Page 15: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/15.jpg)
15
Higher order
F = fun(X) -> X + 1 end.
F(5)
![Page 16: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/16.jpg)
16
Higher order
map(Fun, List) ->case List of [] ->
[]; [H|T] -> [Fun(H) | map(Fun, T)] end.
![Page 17: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/17.jpg)
17
Modules-module(lst).-export([reverse/1]).
reverse(L) ->reverse(L,[]).
reverse(L, A) ->case L of
[] -> A;
[H|T] ->reverse(T,[H|A])
end.
![Page 18: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/18.jpg)
18
Modules
-module(test).-export([palindrome/1]).
palindrome(X) ->case lst:reverse(X) of
X -> yes;_ -> no
end.
![Page 19: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/19.jpg)
19
Concurrency
• Concurrency is explicitly controlled by creation (spawning) of processes.
• A process is when created, given a function to evaluate. – no one cares about the result
• Sending and receiving messages is the only way to communicate with a process.– no shared state (… well, almost)
![Page 20: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/20.jpg)
20
spawning a process
-module(account)
start(Balance) -> spawn(fun() -> server(Balance) end).
server(Balance) -> : : :
![Page 21: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/21.jpg)
21
Receiving messages
server(Balance) ->receive
{deposit, X} ->server(Balance+X);
{withdraw, X} ->server(Balance-X);
quit ->ok
end.
![Page 22: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/22.jpg)
22
Sending messages
:Account = account:start(40),Account ! {deposit, 100},Account ! {withdraw, 50},
:
![Page 23: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/23.jpg)
23
rpc-like communication
server(Balance) ->receive
: : {check, Client} -> Client ! {saldo, Balance}, server(Balance); : : end.
![Page 24: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/24.jpg)
24
rpc-like communication
friday(Account) ->Account ! {check, self()},
receive {saldo, Balance} ->
if Balance > 100 ->
party(Account); true ->
work(Account) end end.
![Page 25: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/25.jpg)
25
Process communication
m1
m2
P1 P2
messages received in FIFO order
asynchronous sending, no acknowledgment
implicit deferral of message delivery
m2
m1
receive deliver
![Page 26: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/26.jpg)
26
Implicit deferral
• A process will have an ordered sequence of received messages.
• The first message that matches one of several program defined patterns will be delivered.
• Pros and Cons– one can select which messages to handle
first– risk of forgetting messages that are left in a
growing queue
![Page 27: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/27.jpg)
27Distributed Systems ID220U
order of messages
src(End, Relay) -> End ! a, Relay ! b.
relay(End) -> receive X -> End ! X end.
src
relay
enda
b b
![Page 28: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/28.jpg)
28
Registration
• A node register associate names to process identifiers.
• Knowing the registered name of a process you can look-up the process identifier.
• The register is a shared data structure!
![Page 29: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/29.jpg)
29
the name is the key :
MyAccount = account:start(400),register(johan, MyAccount),
:
:if Balance > 100 -> party(Account);
true ->johan ! {withdraw, 100),
Account ! {deposit, 100}, party(Account)end
![Page 30: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/30.jpg)
30
Distribution
• Erlang nodes (an Erlang virtual machine) can be connected in a group .
• Each node has a unique name.• Processes in one node can send messages
to and receive messages from processes in other nodes using the same language constructs
![Page 31: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/31.jpg)
31
starting a node
moon> erl -sname gold -setcookie jhgsyt::(gold@moon)>
![Page 32: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/32.jpg)
32
node registry
a process registered as account
a node called gold
global name {account, 'gold@moon'}
a host called moon
![Page 33: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/33.jpg)
33
Failure detection
• A process can monitor another process.– if the process dies a messages is placed
in the message queue
• The message will indicate if the termination was normal or abnormal or ..... if the communication was lost.
![Page 34: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/34.jpg)
34
monitor the account
Ref = erlang:monitor(process, Account),Account ! {check, self()},receive {saldo, Balance} ->
:
{'DOWN', Ref, process, Account, Reason}-> :
end
![Page 35: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/35.jpg)
35
Automatic termination
• A process can link to another process.– if the process dies with an exception the
linked process will die with the same exception
• Processes that depend on each other are often linked together, if one dies they all die.
![Page 36: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/36.jpg)
36
linking
P = spawn_link(fun()-> server(Balance) end),
do_something(P),
![Page 37: Erlang - functional programming in a concurrent world · PDF file2 Erlang • Concurrent Oriented Programming –processes have state –communicate using message passing –access](https://reader030.vdocuments.us/reader030/viewer/2022021504/5abb3e137f8b9a76038c7c6f/html5/thumbnails/37.jpg)
37
Erlang
• the functional subset• concurrency• distribution• failure detection