The Erlang Programming Language
-
Upload
dennis-byrne -
Category
Technology
-
view
2.985 -
download
7
description
Transcript of The Erlang Programming Language
![Page 2: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/2.jpg)
Introduction
• Not a “shiny new object (or function)”• Open sourced by Ericsson in 1998• Functional Programming• Concurrency• Reliability
Erlang is a functional programming language, with native constructs for concurrency and reliability.
![Page 3: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/3.jpg)
Functions as First Class Citizens
Msg = “Lexically scoped closures”,
F = fun()-> erlang:display(Msg) end,
F(). % prints “Lexically scoped closures”
![Page 4: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/4.jpg)
Single Assignment & Unification
I = 4. % assignmentI = I. % unification (invariant)4 = I. % unification (invariant)I = 5. % throws error
MyList = [“A”]. [x] = MyList. % implicit declaration erlang:display(X). % prints “A”
![Page 5: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/5.jpg)
Tail Recursion
loop() ->erlang:display(“in constant
space”), loop().
• Automatic byte code manipulation• Runs in constant space• Last call optimization• for, do and while are the new malloc• Tail recursion is to ‘recursion’ as optimistic locking is to ‘locking’
![Page 6: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/6.jpg)
Arbitrary Precision
Math in Erlang behaves correctly
Sum = 1111111111111111111111111111 + 1.
![Page 7: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/7.jpg)
Currying, Memoization and Monads
![Page 8: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/8.jpg)
What is an Erlang Process?
• An Erlang process (misnomer) combines the best of– An operating system process– An operating system thread– A “green thread”
• Controlled in user space by the ERTS• Costs less than 300 bytes
– Private heap– Private stack
• Pre-emptive Scheduling• Incremental garbage collection• Private immutable state
Most important slide in this presentation
![Page 9: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/9.jpg)
The Actor Model
• Asynchronous message passing• Messages passed by value, not reference• One to one relationships
– An Actor– A mailbox– A Process– A Process ID, or pid
![Page 10: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/10.jpg)
Concurrency Primitives: spawn
A built in function used to create a process.
Pid = spawn(Node, Module, Function, Args)
![Page 11: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/11.jpg)
Concurrency Primitives: the send operator
!
![Page 12: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/12.jpg)
Concurrency Primitives: sending
The send operator passes a message to a process. It is “fire and forget”.
Msg = { 4, true, “B” },Pid ! Msg.
![Page 13: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/13.jpg)
Concurrency Primitives: receive
receivePattern1 [ when Guard1 ] ->
dosomething().Pattern2 [ when Guard2 ] ->
dosomethingelse().end
![Page 14: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/14.jpg)
Reliability
• Open Telecom Platform - Supervisor Trees• Monitor
– monitor_node(Node, Bool)– Unidirectional
• Link– Pid = spawn_link(Node, Module, Fun, Args)– Bi-directional
• Sequential Constructs– try– catch
![Page 15: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/15.jpg)
Reliability (Continued)
• Erlang can run two and only two versions of a module• Code running has a version lifecycle
– Current– Old– Dead
• Function definitions can be upgraded in the middle of a tail recursive call
• This VM could do this before closures were supported• Ericsson claims nine nines uptime on AX3D01
– 2 million lines of Erlang– Five nines = 5.2 minutes of downtime/year
![Page 16: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/16.jpg)
Open Telecom Platform Behaviours
• Basic Design Principles– Interface– Design Pattern
• Supervision Trees• Generic Servers• Finite State Machines• Event Manager
![Page 17: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/17.jpg)
Supervisor Behaviour
-module(byrne_sup).-behaviour(supervisor).-export([start_link/1, init/1]).
start_link(Args) -> supervisor:start_link({local, ?MODULE}, ?MODULE, Args).
init(_Args) ->{ok, {{one_for_one, 10, 10},
[{byrne, {byrne, start_link, []}, permanent, 2000, worker, [byrne]}]}}.
![Page 18: The Erlang Programming Language](https://reader033.fdocuments.in/reader033/viewer/2022061613/554f48cbb4c905524c8b47f3/html5/thumbnails/18.jpg)
Generic Server Behaviour (abbreviated)
-module(byrne).-behaviour(gen_server).
start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, self(), []).
handle_call(Function, _From, State) -> {reply, erlang:apply(rest, Function, []),, State + 1}.
handle_cast(_Msg, State) -> {noreply, State}.