View
2.993
Download
7
Category
Tags:
Preview:
DESCRIPTION
http://notdennisbyrne.blogspot.com/2009/09/presenting-erlang-at-polyglot.html
Citation preview
Dennis Byrnedennisbyrne@apache.org
The Erlang Programming Language
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.
Functions as First Class Citizens
Msg = “Lexically scoped closures”,
F = fun()-> erlang:display(Msg) end,
F(). % prints “Lexically scoped closures”
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”
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’
Arbitrary Precision
Math in Erlang behaves correctly
Sum = 1111111111111111111111111111 + 1.
Currying, Memoization and Monads
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
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
Concurrency Primitives: spawn
A built in function used to create a process.
Pid = spawn(Node, Module, Function, Args)
Concurrency Primitives: the send operator
!
Concurrency Primitives: sending
The send operator passes a message to a process. It is “fire and forget”.
Msg = { 4, true, “B” },Pid ! Msg.
Concurrency Primitives: receive
receivePattern1 [ when Guard1 ] ->
dosomething().Pattern2 [ when Guard2 ] ->
dosomethingelse().end
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
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
Open Telecom Platform Behaviours
• Basic Design Principles– Interface– Design Pattern
• Supervision Trees• Generic Servers• Finite State Machines• Event Manager
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]}]}}.
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}.
The Erlang Programming Language
ThanksDennis Byrne – DRW Trading
dennisbyrne@apache.org
Recommended