kth royal institute of technology erlang functional
TRANSCRIPT
![Page 1: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/1.jpg)
KTH ROYAL INSTITUTE
OF TECHNOLOGY
Erlang – functional programming in
a concurrent world Johan Montelius and Vladimir Vlassov
![Page 2: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/2.jpg)
Concurrent Oriented Programming
• processes have state
• communicate using
message passing
• access and location
transparent
• asynchronous
Erlang
2 ID2201 DISTRIBUTED SYSTEMS / ERLANG
Functional programming
• evaluation of expressions
• recursion
• data structures are
immutable
![Page 3: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/3.jpg)
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.
3 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 4: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/4.jpg)
Today
4 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 5: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/5.jpg)
Why Erlang?
• concurrency built-in
• multicore performance
• simple to implement fault tolerance
• scales well in distributed systems
5 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 6: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/6.jpg)
Erlang
• the functional subset
• concurrency
• distribution
• failure detection
6 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 7: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/7.jpg)
Data structures
• Literals
• atoms: foo, bar, ...
• numbers: 123, 1.23 ..
• bool: true, false
• Compound structures
• tuples: {foo, 12, {bar, zot}}
• lists: [], [1,2,foo,bar]
7 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 8: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/8.jpg)
Variables
• lexically scoped
• implicit scoping – the procedure definition
• untyped – assigned a value when introduced
• syntax: X, Foo, BarZot, _anything
8 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 9: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/9.jpg)
Assignment – 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, C}} = {41, {foo, bar}} {A, {B, A}} = {41, {foo, 41}}
9 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 10: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/10.jpg)
Pattern matching
Pattern matching is used to extract elements from a data structure.
{person, Name, Age} = find_person(Id, Employes),
10 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 11: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/11.jpg)
Pattern matching
Pattern matching can fail:
{person, Name, Age} = {dog, pluto}
11 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 12: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/12.jpg)
No circular structures
You can not construct circular data structures in Erlang.
(a structure in which the last element is a pointer to the first)
Pros – makes the implementation easier.
Cons – Someone might like/need circular structures.
12 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 13: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/13.jpg)
Definitions
area(X, Y) -> X * Y.
13 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 14: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/14.jpg)
if statement
fac(N) -> if N == 0 -> 1; N > 0 -> N*fac(N-1) end.
14 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 15: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/15.jpg)
case statement
sum(L) -> case L of [] -> 0; [H|T] -> H + sum(T) end.
15 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 16: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/16.jpg)
case statement
member(X,L) -> case L of [] -> no; [X|_] -> yes; [_|T] -> member(X, T) end.
16 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 17: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/17.jpg)
Higher order
F = fun(X) -> X + 1 end. F(5)
17 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 18: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/18.jpg)
Higher order
map(Fun, List) -> case List of [] -> []; [H|T] -> [Fun(H) | map(Fun, T)] end.
18 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 19: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/19.jpg)
Modules
-module(lst). -export([reverse/1]). reverse(L) -> reverse(L,[]). reverse(L, A) -> case L of [] -> A; [H|T] -> reverse(T,[H|A]) end.
19 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 20: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/20.jpg)
Modules
-module(test). -export([palindrome/1]). palindrome(X) -> case lst:reverse(X) of X -> yes; _ -> no end.
20 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 21: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/21.jpg)
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)
21 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 22: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/22.jpg)
Spawn a process
-module(account) start(Balance) -> spawn(fun() -> server(Balance) end). server(Balance) -> : : :
22 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 23: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/23.jpg)
Receiving a message
server(Balance) -> receive {deposit, X} -> server(Balance+X); {withdraw, X} -> server(Balance-X); quit -> ok end.
23 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 24: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/24.jpg)
Sending a message
: Account = account:start(40), Account ! {deposit, 100}, Account ! {withdraw, 50}, :
24 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 25: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/25.jpg)
RPC-like communication
server(Balance) -> receive : {check, Client} -> Client ! {saldo, Balance}, server(Balance); : end.
25 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 26: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/26.jpg)
RPC-like communication
friday(Account) -> Account ! {check, self()}, receive {saldo, Balance} -> if Balance > 100 -> party(Account); true -> work(Account) end end.
26 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 27: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/27.jpg)
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
27 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 28: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/28.jpg)
Registration
A node registers associate names to process identifiers.
register(alarm_process, Pid)
Knowing the registered name of a process you can look-up
the process identifier.
The register is a shared data structure!
28 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 29: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/29.jpg)
Registration
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
29 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 30: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/30.jpg)
Starting a node
moon> erl -sname gold -setcookie xxxx
:
:
(gold@moon)>
30 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 31: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/31.jpg)
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
31 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 32: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/32.jpg)
Monitor
Ref = erlang:monitor(process, Account),
Account ! {check, self()},
receive
{saldo, Balance} ->
:
{’DOWN’, Ref, process, Account, Reason}->
:
end
32 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 33: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/33.jpg)
Atomic 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.
33 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 34: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/34.jpg)
Linking
P = spawn_link(fun()-> server(Balance) end),
do_something(P),
34 ID2201 DISTRIBUTED SYSTEMS / ERLANG
![Page 35: KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b38ebd0c406617737bbf/html5/thumbnails/35.jpg)
Summary
• functional programming
• processes
• message passing
• distribution
• monitor/linking
35 ID2201 DISTRIBUTED SYSTEMS / ERLANG