19
Dennis Byrne [email protected] The Erlang Programming Language

The Erlang Programming Language

Embed Size (px)

DESCRIPTION

http://notdennisbyrne.blogspot.com/2009/09/presenting-erlang-at-polyglot.html

Citation preview

Page 1: The Erlang Programming Language

Dennis [email protected]

The Erlang Programming Language

Page 2: 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.

Page 3: The Erlang Programming Language

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

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

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

Arbitrary Precision

Math in Erlang behaves correctly

Sum = 1111111111111111111111111111 + 1.

Page 7: The Erlang Programming Language

Currying, Memoization and Monads

Page 8: The Erlang Programming Language

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

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

Concurrency Primitives: spawn

A built in function used to create a process.

Pid = spawn(Node, Module, Function, Args)

Page 11: The Erlang Programming Language

Concurrency Primitives: the send operator

!

Page 12: The Erlang Programming Language

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

Concurrency Primitives: receive

receivePattern1 [ when Guard1 ] ->

dosomething().Pattern2 [ when Guard2 ] ->

dosomethingelse().end

Page 14: The Erlang Programming Language

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

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

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

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

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}.

Page 19: The Erlang Programming Language

The Erlang Programming Language

ThanksDennis Byrne – DRW Trading

[email protected]