43
ERLANG THE WORLD IS PARALLEL. Picture: http://www.cs.purdue.edu/homes/jv/events/TiC06/pr.jpg

Erlang, an overview

Embed Size (px)

DESCRIPTION

An overview presentation about Erlang, the concurrent programming language

Citation preview

Page 1: Erlang, an overview

ERLANG THE WORLD IS PARALLEL.

Picture: http://www.cs.purdue.edu/homes/jv/events/TiC06/pr.jpg

Page 2: Erlang, an overview

the world is parallel.

If we want to write programs that behave as other objects behave in the real world, then these programs will have a concurrent structure.

Use a language that was designed for writing concurrent applications, and development becomes a lot easier.

Erlang programs model how we think and interact.

Joe Armstrong

Page 3: Erlang, an overview

Agenda   What we cover   What we don’t cover   You used Erlang to write what?   History   OTP   The language   Concurrency   Distribution   Hot code swapping   Compile and run   Development environments   Various tidbits

Page 4: Erlang, an overview

Note and forgive that…

  This is an Erlang beginner trying to give you an overview

  I haven’t used Erlang for a while

Page 5: Erlang, an overview

What we cover

  What is it good for?   Glance at functional programming   Basic syntax and structure   Erlang’s main features   Basic tools to get you started   Info on where to find more

material

Page 6: Erlang, an overview

What we DON’T cover

  How to install Erlang   A (blog) tutorial in 15 minutes   Language reference   How to write programs using a

functional programming language   How to parallelize programs   How to distribute algorithms   How to use the Open Telecom Platform   Tons of example code

Page 7: Erlang, an overview

Therefore

  Read the book

Page 8: Erlang, an overview

You used Erlang to write what?

http://www.cio.com/article/191000/You_Used_Ruby_to_Write_WHAT_ http://www.zedshaw.com/

Page 9: Erlang, an overview

You used Erlang to write what?

  Systems/Applications that are:  Concurrent  Parallel (SMP)  Distributed  Fault tolerant  Dynamically upgradable

Page 10: Erlang, an overview

History

  Created by the Computer Science Laboratory at Ellemtel (now Ericsson AB) around 1990

  Named after A. K. Erlang (not Ericsson Language)

  Licensed Version (Ericsson http://www.erlang.se)

  Released as open source in 1998 under EPL (http://www.erlang.org)

Ref: wikipedia.org

Page 11: Erlang, an overview

Open Telecom Platform

  Middleware   Similar to J2EE Container (JBoss, GlassFish,

IBM WebSphere, BEA Weblogic)   Coding standards   Tools like web server, FTP server and CORBA

ORB are included in OTP   Licensed under Erlang Public License (EPL)

Page 12: Erlang, an overview

OTP

Source: http://www.ericsson.com/technology/opensource/erlang/index.shtml Picture: http://www.ericsson.com/technology/opensource/images/erlang.gif

Page 13: Erlang, an overview

Erlang in use

  Ejabberd (http://www.ejabberd.im)   Yaws (http://yaws.hyber.org/)   CouchDB (

http://incubator.apache.org/couchdb)   Ericsson products AXD301 (ATM switch),

SGSN, GGSN (GPRS nodes) according to http://www.erlang.se/productinfo/index.shtml

Page 14: Erlang, an overview

The language

  Functional programming language!   (little) to no side effects  Single assignment variables  Pattern matching  Higher-order functions  Recursion  No objects  Haskell, Lisp, ML, F#, Scheme

Page 15: Erlang, an overview

So don’t think imperative!

  Don’t think  Java  C#  C  PHP  Etc.

Page 16: Erlang, an overview

Think

Functional!

Page 17: Erlang, an overview

Don’t think

Procedural

Page 18: Erlang, an overview

Think

Recursive!

Page 19: Erlang, an overview

Don’t think

Loopy

Page 20: Erlang, an overview

Let‘s get started

Picture: http://www.wampower.com/images/wam%20pics/getting%20started/getting_started.jpg

Page 21: Erlang, an overview

Let’s get started

  Start interactive shell called erl and try things out (like irb in Ruby)

  ^G q to quit the shell   % indicates a comment   . terminates a statement

Page 22: Erlang, an overview

Files, modules and functions

  Example on next slide   Module “geometry” would be defined in

“geometry.erl”   Export the functions you want to be public   Don’t forget to specify the arity, number of params

in function’s signature (e.g area/1)   Compile modules within erl before using them

c(module_name)   Use : to access a module’s public functions =>

lists:filter()   erl –man module_name for man page => erl -man

lists

Page 23: Erlang, an overview

Variables

  Start with an uppercase letter   Single assignment like in Prolog so X = X + 1 is a

no go   Integers

 No limitation for integers e.g. faculty of 2000 returns valid result (Try that in Eiffel™)

  Floats  A division always returns a float   Limited in size (IEEE 754 64-bit)

  Strings: S = “world” (double quotes)

Page 24: Erlang, an overview

Atoms and Tuples

  Atoms  Represent non numerical constant values  Start with lowercase letter

  Tuples  Store a fixed number of items into a single entity  F = {name , isabella}  E = {woweffect, 10}  Person = {person,{height,10},{gender,male}}  Beauty = {beauty , F , E}

Page 25: Erlang, an overview

Lists

  Store variable numbers of things   Head and tail syntax   Define a list:

 Student_grades = [{john,c},{michael,b},{lolita,aplus},{joe,b}].

  Extract into head and tail   [Headval1,Headval2|Tailval] = Student_grades  Where Headval1 matches with the 1st element,

Headval2 with 2nd and Tailval with the rest   See erl –man lists

http://images.apple.com/downloads/dashboard/travel/images/traveltodolist_20070724165034.jpg

Page 26: Erlang, an overview

Functions -module(intro).

-export([main/0,factorial/1,some_other_function/0]).

% number in export statement defines arity of exported function

% only exported functions can be used from outside the module

main() -> io:format("The factorial of 5 is ~w ~n”,[factorial(5)]).

factorial(0) -> 1;

factorial(nonsense) -> "nonsense";

factorial(N) -> N*factorial(N-1).

% use comma to add statements together

some_other_function() -> T = private_module_function(10,20),

T + 11.

% this function is not available from outside the module

private_module_function(X,Y) -> X * Y.

Picture: http://casoilresource.lawr.ucdavis.edu/drupal/files/images/logistic-small.png

Page 27: Erlang, an overview

Anonymous functions (Funs)

http://image.guardian.co.uk/sys-images/Arts/Arts_/Pictures/2007/08/10/laugh460.jpg

Page 28: Erlang, an overview

Anonymous functions (Funs)

  Like in JavaScript, delegates in C#   Like regular functions but the word “end” defines the

end of the function. Still use a period to indicate end of line.

  Cubic = fun(X) -> X*X*X end.   Cubic(2) evaluates to 8.   Funs can be useful as arguments eg. inline

  lists:filter(fun(X) -> X > 10 end, [1,2,3,445,28,203]).   Returns [445,28,203]

  It is legal to pass a variable containing a Fun instead of defining it inline

  Funs can return Funs

Page 29: Erlang, an overview

List comprehensions

  Mostly shorter than funs, maps and filters   To triple every element using funs we could

write:  Lists:map(fun(N) -> 3*N end, L)

  Using list comprehensions   [ 3*N || N <- L]

  More complex forms are possible   [ {triple, 3*N} || N <- L]  According to the rules of pattern matching

Page 30: Erlang, an overview

Stuff left out to save time   Guards => Conditions

  f(X) when is_integer(X), X > 10 -> io:format(“X is greater than 10”).

  Predicates like is_integer()   Built-in functions like length(X)

  Records (for large tuples)   ETS / DTS   case and if Expressions   Exceptions (try catch syntax)   Built-in Functions (BIFs)

  http://www.erlang.org/doc/man/erlang.html   Macros

Picture: http://www.ourappraisal.com/xsites/Appraisers/centralilappraisal/content/uploadedFiles/refi_clock_ticking.jpg

Page 31: Erlang, an overview

Concurrency

  Processes belong to Erlang and not the operating system (User, not kernel space)

  Creating and destroying processes is very fast   Sending messages between processes is very

fast   Processes behave the same way on all operating

systems   We can have a large number of processes.   Processes share no memory and are completely

independent   The only way for processes to interact is through

message passing

Page 32: Erlang, an overview

Concurrency

  Concurrency primitives   spawn Pid = spawn(Fun)

  send Pid ! Message

  receive receive

Pattern 1 Expressions1;

Pattern 2 Expressions 2;

... end

Picture: http://www.spacecast.com/images/Shows/spawn_img1.jpg

Page 33: Erlang, an overview

Distribution

  Distributed Erlang  Programs are written to run on Erlang nodes  Spawning processes on any node is very easy  Message passing and error handling is the same as in

a non distributed environment (one single node).  The nodes run in a trusted environment (same LAN)  Make sure, you run the same codebase and erlang

version   Socket-Based Distribution

 Using TCP/IP Sockets  The Applications can run in a untrusted environment

Page 34: Erlang, an overview

Hot Code Swapping

Picture: http://www.firewebdesigns.com/images/fire_02.jpg

Page 35: Erlang, an overview

Hot Code Swapping

  Erlang supports change of code in a running system  Very cool for upgrading software

  Code for Module exist in two variants in the system  State Current  State Old

  OTP provides libraries for enhanced Code Swapping functionality

Page 36: Erlang, an overview

Multicore CPU’s

  Again, in Erlang, processes have no shared memory, so no mutexes, locks whatsoever

  Make sure, your program has a lot of processes   Avoid side effects (those that the language and

the runtime can’t prevent)   In fact, Erlang has shared data structures

(ETS,DETS). Better use Mnesia with transactions if you need a shared storage.  Mnesia is a distributed DBMS for Erlang   http://www.erlang.org/documentation/doc-5.0.1/lib/

mnesia-3.9.2/doc/index.html

Page 37: Erlang, an overview

Symmetric Muliprocessing with SMP Erlang   erl –smp +S N

 Where N specifies the number of schedulers to use

 A Scheduler is a complete virtual machine  Possibility to emulate a system with more CPU’s

  See previous slide for some requirements to parallelize a program

Page 38: Erlang, an overview

Compile and run

  Within erl shell  c(module_name)

  Compiler  erlc module_name.erl  erl –noshell –s module_name function_name –s

init stop   As an escript

#!/usr/bin/env escript main(_) ->

io:format("hello world~n").

  Automate things using build tools like make, ant etc.

Page 39: Erlang, an overview

Development environment

  http://www.erlang.org/download.html to install environment

  Erlang mode for emacs   http://www.erlang.org/doc/apps/tools/

erlang_mode_chapter.html   Erlang bundle for TextMate

  http://macromates.com/svn/Bundles/trunk/Bundles/Erlang.tmbundle/

  Erlybird for Netbeans   http://sourceforge.net/projects/erlybird/

  Erlide   http://erlide.sourceforge.net/

Page 40: Erlang, an overview

Various tidbits   Documentation generator called Edoc which is similar to Javadoc™

  http://www.erlang.org/doc/apps/edoc/index.html   When things go wrong, consult the crash dump.

  webtool:start().   Go to http://localhost:8888

  Unit testing framework called EUnit making heavy use of macros for the assertions   http://svn.process-one.net/contribs/trunk/eunit/doc/overview-

summary.html   ErlyWeb, a web framework written in Erlang

  http://yarivsblog.com   The Dialyzer, a static analysis tool to identify problems with program

code   http://www.it.uu.se/research/group/hipe/dialyzer/

Page 41: Erlang, an overview

Where to find help?

  Excellent documentation and examples on erlang.org  http://www.erlang.org/starting.html  http://www.erlang.org/doc.html  http://www.erlang.org/doc/reference_manual/

part_frame.html  http://www.erlang.org/examples.html

  Joe Armstrongs home page  http://www.sics.se/~joe/

  Consult erl –man module_name

Page 42: Erlang, an overview

Further reading

  Open Source Erlang   http://www.erlang.org

  The High-Performance Erlang Project   http://www.it.uu.se/research/group/hipe/

  Trapexit, an Erlang community site   http://www.trapexit.org/

  Comprehensive Erlang Archive Network   http://cean.process-one.net/

  Erlang style concurreny   Article about concurrency explaining important topics using

Java syntax to explain things   http://www.defmacro.org/ramblings/concurrency.html

Page 43: Erlang, an overview

Further reading

  Programming Erlang, Software for a Concurrent World, Joe Armstrong (Pragmatic Bookshelf, 2007), ISBN 978-1-934356-00-5, Available in print and PDF

  Erlang: The Movie ;-)  http://video.google.com/videoplay?

docid=-5830318882717959520