Top Banner
Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden [email protected] http://www.ericsson.se/cslab/~thomas
35

Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden [email protected] thomas.

Dec 14, 2015

Download

Documents

Ezra Towson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Industrial Use of a Functional Language

Thomas ArtsEricsson

Computer Science Laboratory

Stockholm, Sweden

[email protected]://www.ericsson.se/cslab/~thomas

Page 2: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Telecom industry

• Switches, routers,

base-stations

• Networks

• Mobile telephones

Page 3: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Computer ScienceLaboratory

• Founded 1983

• Research on implementation tools, methods and techniques for telecommunication applications• Intermediate between universities and Ericsson product units

Page 4: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Telecom requirements

Requirements of telecom software

• Concurrency

• Distribution

• Soft real-time

• Robust

• Non-stop system

• External interfaces

Page 5: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

A good language?

Experiments in the 80s with Ada, C, ML, CML, Prolog...

• large programs (million lines)• change code in running system• fast message passing• low memory overhead• no memory fragmentation/leaks• recover from errors

Page 6: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlang/OTP

A functional language successfully used for programming large real-time control systems.

OTP is the set of libraries that is used with Erlang for product development

Page 7: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlang/OTP

• Erlang/OTP develop/maintenance• Erlang consultancy & courses• Erlang used many systems, e.g. ATM switch and new GSM network (GPRS)• Erlang Open Source

Page 8: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Erlangsequential program

Thomas Arts

-module(math).

-export([fac/1]).

fac(N) when N>0 ->

N*fac(N-1);

fac(N)->

1.

Page 9: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangdatatypes

• atoms (true,foo,’Hello’)• numbers (1212864187154)• floats (3.141592)• tuples ({a,123})• lists ([1,123,2,56])• process identifiers• ...

Page 10: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangdatatypes

• dynamically typed language• poor mechanism to build your own datatypes

Page 11: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcontrol structures

Matching

case X of

{ok,List} -> hd(List);

{resend,Data} -> submit;

error -> exit(error);

_ -> retry(X)

end

Page 12: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcontrol structures

Guards

f(....) when guard -> ...

If

f(X) ->

if guard1 -> ...;

guard2 -> ...

end

Page 13: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcontrol structures

Higher order functions

f(F,X) -> F(X);

map(F,[1,2,3,4]).

List comprehensions

[ X || {X,Y}<-Set, guard(X)]

Page 14: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcontrol structures

Naming of objects/data

f(X) ->

Dev = update_device(X),

{Date,Time} = now(),

h({Dev,Date}).

Page 15: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcontrol structures

Sequence

f(X) ->

action1(X),

action2(X);

update(X) ->

log(X,”myfile”), new(X).

side-effects

Page 16: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcontrol - concurrency/distribution

Creating a process

Pid = spawn(F,[Arg1,...,ArgN]);

P1 P2

B = spawn(F,Args);P2

F(Arg1,...,ArgN)

Page 17: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcontrol - concurrency/distribution

Sending messages

Pid ! Message;

P1 P2

B!{self(),hej};

{P1,hej}{P1,hej}

Page 18: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcontrol - concurrency/distribution

Receiving messages

receive

Pattern -> ...;

end;

P1 P2{P1,hej}

receive {From,Msg} -> From ! {ok,Msg}end

P1 ! {ok,hej}

Page 19: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangchanging code in running system

P0

loop(F) -> receive {exec,Pid,Arg} -> Pid!F(Arg), loop(F) end;

P1N = F(15),P0 ! {exec,self(),15},N = receive Answer -> Answer end,

loop(F) -> receive {change,G} -> loop(G); {exec,Pid,Arg} -> Pid!F(Arg), loop(F) end;

Page 20: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

Processes can be linked to each other:

PidA PidB

Links are created by using either:

• link(Pid), or

• spawn_link(Module, Function, Args)

Links are bi-directional.

They can be removed using unlink(Pid).

Page 21: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

When a process terminates, an exit signal is sent to all processes the process is linked to:

PidA PidBPidA

A process can terminate normally, due to a run-time error, or when explicitly ordered to do so.

Page 22: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

PidC PidD PidEPidA PidBPidB PidD

If a process terminates abnormally, the emitted exit signal will (by default) cause the recipient to terminate:

The termination reason in the transmitted exit signals will be the same as in the received one (the exit signal is propagated).

Page 23: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

exit(error)

PidB PidCerror

PidAerror

A process can terminate itself using exit(Reason). This will cause exit signals with termination reason Reason to be emitted.

Page 24: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

A process can explicitly send an exit signal to another process using exit(Pid, Reason):

exit(PidB, error)

PidBPidAerror

• The calling process is not affected.

• The processes do not need to be linked.

Page 25: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

A process can trap exit signals using:process_flag(trap_exit, true).

• Incoming exit signals will be transformed into messages of the form: {'EXIT', Pid, Reason}

• These exit messages are delivered to the process mailbox in the normal way.

Page 26: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

PidC terminates with reason error, PidD is trapping exits:

PidC PidD PidEPidA PidBerror error

PidBPidA

PidB terminates, propagating the exit signal. PidD will receive an exit message

{'EXIT', PidC, error}.

Page 27: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

Robust systems can be made by layering.

Page 28: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangfault tolerance

supervision trees and restart strategies

P1 P2 P3 P1 P4 P3

P6P5P1 P2 P3 P4

Page 29: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlangcomponent based development

recognize frequently occurringpatterns and transfer them intostandard components.

• faster development• uniform code (maintenance)• less errors

Page 30: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlang

Developed with the application of the language constantly in mind

Practical usability more priority than purity

Page 31: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlang is used

AXD 301, scalable ATM switch (up to 160 GB/sec)

• four years work• more than 500,000 lines of Erlang• several hundreds of programmers

Page 32: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlang is used

GPRS: next generation GSM network.

• Eigth times faster internet access in mobile phones• always connected• development in three countries, hundreds of people

Page 33: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlang is used

The success of the language in industry is due to

• Language features

• Support and libraries

• Design patterns / architectures

Page 34: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlang is used

Become a user yourself!

• passive: make a phonecall

• active: download Erlang for free

over 300 downloads

per month

Page 35: Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden thomas@cslab.ericsson.se thomas.

Thomas Arts

Erlang

The functional language

for industry