1 / 45

Introduction to Erlang

Introduction to Erlang. Eddy Caron 2013 M1. ENS-Lyon. 1. Piece of History.

preston
Télécharger la présentation

Introduction to Erlang

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Erlang Eddy Caron 2013 M1. ENS-Lyon 1

  2. Piece of History 1982 – 1985 Experiments with programming of telecom using > 20 different languages. Conclusion: The language must be a very high level symbolic language in order to achive productivity gains ! (Leaves us with: Lisp , Prolog , Parlog ...) 1985 – 86 Experiments with Lisp, Prolog, Parlog etc. Conclusion: The language must contain primitives for concurrency and error recovery, and the execution model must not have back-tracking. (Rules out Lisp and Prolog). We must therefore develop our own language with the desirable features of Lisp, Prolog and Parlog, but with concurrency and error recovery built into the language. 1987 The first experiments with Erlang. 1993 Distribution is added to Erlang, which makes it possible to run a homgeneous Erlang system on a heterogeneous hardware. Decision to sell implementations Erlang externally. Separate organization in Ericsson started to maintain and support Erlang implementations and Erlang Tools. 2

  3. The Erlang Shell • The Erlang Shell • You can use Ctrl+c to break [ecaron@aspen bin]$ ./erl Erlang R15B02 (erts-5.9.2) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.2 (abort with ^G) 1> 2+5. 7 2> (42+6)*33/4. 396.0 3> halt(). [ecaron@aspen bin]$ BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded (v)ersion (k)ill (D)b-tables (d)istribution 3

  4. Hello world • ‘%’ starts a comment • ‘.’ ends a declaration • Every function must be in a module • one module per source file • source file name is module name + “.erl” • ‘:’ used for calling functions in other modules 4

  5. Module and Functions • A programming language isn't much use if you can just run code from the shell. • use it [ecaron@aspen Erlang]$ cat mult2.erl -module(mult2). -export([double/1]). double(X) -> 2 * X. [ecaron@aspen Erlang]$ erl Erlang R15B02 (erts-5.9.2) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.2 (abort with ^G) 1> c(mult2). {ok,mult2} 2> mult2:double(6). 12 Compilation is ok ! 5

  6. Numbers • Regular numbers • #-notation for base-N integers • $-notation for character codes (ISO-8859-1) 123-34567 12.345 -27.45e-05 1> 16#ff. 255 2> $A. 65 6

  7. Atoms • Must start with lower case character or be quoted • Similar to hashed strings • use only one word of data • constant-time equality test friday unquoted_atoms_cannot_contain_blanks ’A quoted atom with several blanks’ ’hello \n my friend’ 7

  8. Tuples • Terms seprated by ‘,’ and enclosed in {} • A fixed number of items (similar to structure or record in conventional programming languages) • A tuple whose first element is an atom is called a tagged tuple {123, bcd} {123, def, abc} {person, 'Jax', ’Teller'} {abc, {def, 123}, jkl} {} 8

  9. Other Data Types Functions Binaries Process identifiers References: A reference is a term which is unique in an Erlang runtime system, created by calling make_ref/0. Erlang values in general are called « terms » All terms are ordered and can be compared with ‘<’, ‘>’, ‘==’, ‘=:=’ , etc. Module:fun(Arg1,Arg2,… Argn) Bin = <<Bin0,...>> 1> spawn(m, f, []). <0.51.0> 9

  10. Recursive Functions Variables start with upper-case characters ‘;’ separates function clauses ‘,’ separates instructions Variables are local to the function clause Pattern matching and guards to select clauses 10

  11. Recursive Functions • Compile and run recurs.erl -module(recurs). -export([fac/1]). fac(0) -> 1; fac(N) -> N * fac(N-1). [ecaron@aspen Erlang]$ erl Erlang R15B02 (erts-5.9.2) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.2 (abort with ^G) 1> c(recurs). {ok,recurs} 2> recurs:fac(6). 720 11

  12. Recursive Functions • Compile and run dblfunc.erl -module(dblfunc). -export([fac/1, mult/2]). fac(1) -> 1; fac(N) -> N * fac(N - 1). mult(X, Y) -> X * Y. [ecaron@aspen Erlang]$ erl Erlang R15B02 (erts-5.9.2) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.2 (abort with ^G) 1> c(dblfunc). {ok,dblfunc} 2> dblfunc:fac(4). 24 3> dblfunc:mult(dblfunc:fac(4),2). 48 12

  13. Tail Recursion The arity is part of the function name Non-exported functions are local to the module mylists.erl -module(mylists). -export([reverse/1]). reverse(L) -> reverse(L, []). reverse([H|T], L) -> reverse(T, [H|L]); reverse([], L) -> L. > mylists:reverse([3,2,1]). [1,2,3] 13

  14. Tail Recursion > mylists:reverse([1,2,3]). [3,2,1] reverse([1,2,3]) -> reverse([1,2,3],[]). -> reverse([2,3],[1]) -> reverse([3],[2,1]) -> reverse([],[3,2,1]) -> [3,2,1] mylists.erl -module(mylists). -export([reverse/1]). reverse(L) -> reverse(L, []). reverse([H|T], L) -> reverse(T, [H|L]); reverse([], L) -> L. 14

  15. Recursion over Lists Pattern-matching selects components of the data ‘_’ is a “don’t care” pattern (not a variable) ‘[]’ is the empty list ‘[X,Y,Z]’ is a list with exactly three elements ‘[X,Y,Z|Tail]’ has three or more elements 15

  16. List Recursion with Accumulator The same syntax is used to construct lists Strings are simply lists of character codes Avoid adding data to the end of the list 16

  17. Built-in Functions Implemented in C All the type tests and conversions are BIFs Most BIFs (not all) are in the module “erlang” Many common BIFs are auto-imported (recognized without writing “erlang:...”) Operators (‘+’,’-’,’*’,’/’,...) are also really BIFs Described in the BIFS manual 17

  18. Built-in Functions Some examples 1> date(). {2012,10,23} 2> time(). {1,14,35} 3> length([1,2,3,4,5]). 5 4> size({a,b,c}). 3 5> atom_to_list(an_atom). "an_atom" 6> list_to_tuple([1,2,3,4]). {1,2,3,4} 7> integer_to_list(3412). "3412" 8> tuple_to_list({}). [] 18

  19. Standard Libraries • Application Libraries • Kernel • erlang • code • file • inet • os • Stdlib • lists • dict • sets • ... 19

  20. Expressions Boolean and/or/xor are strict (always evaluate both arguments) Use andalso/orelse for short circuit evaluation ‘==’ for equality, not ‘=’ Always use parentheses when not absolutely certain about the precedence 20

  21. Fun Expressions 1> Fun1 = fun (X) -> X+1 end. #Fun<erl_eval.6.39074546> 2> Fun1(2). 3 3> Fun2 = fun (X) when X>=5 -> gt; (X) -> lt end. #Fun<erl_eval.6.39074546> 4> Fun2(7). gt 21

  22. Pattern Matching • Match failure causes run-time error • Successful matching binds the variables • but only if they are not already bound to a value • previously bound variables can be used in a pattern • a new variable can also be repeated in a pattern -module(pm). -export([mylength/1]). mylength([]) -> 0; mylength([_|T]) -> mylength(T) + 1. 22

  23. Case-switches • Any number of clauses • Patterns and guards, just as in functions • ‘;’ separates clauses • Use ‘_’ as catch-all • Variables may also begin with underscore • signals “I don’t intend to use this value” is_valid_signal(Signal) -> case Signal of {signal, _What, _From, _To} -> true; {signal, _What, _To} -> true; _Else -> false end. 23

  24. If-switches • Like a case-switch without the patterns and the ‘when’ keyword • Use ‘true’ as catch-all factorial(N) when N == 0 -> 1; factorial(N) when N > 0 -> N * factorial(N - 1). 24

  25. Switching • If • Case • Pattern Matching • When factorial(0) -> 1; factorial(N) -> N * factorial(N-1). factorial(N) -> if N == 0 -> 1; N > 0 -> N * factorial(N - 1) end. factorial(N) -> 1 case (N) of 0 -> 1; N when N > 0 -> N * factorial(N - 1) end. factorial(N) when N == 0 -> 1; factorial(N) when N > 0 -> N * factorial(N - 1). 25

  26. List and Tuple Processing • List Processing Functions • Tuple Processing BIFS List Processing BIFs atom_to_list(A) float_to_list(F) integer_to_list(I) tuple_to_list(T) list_to_atom(L) ... hd(L) tl(L) length(L) member(X,L) append(L1,L2) reverse(L) delete_all(X,L) tuple_to_list(T) element(N,T) setelement(N,T,Val) size(L) … 26

  27. Catching Exceptions throw: user defined error: runtime errors exit: end process only catch throw exceptionsnormally -module(catch_it). -compile(export_all). % An example of throw and catch g(X) when X >= 13 -> ok; g(X) when X < 13 -> throw({exception1, bad_number}). % Throw in g/1 % Catch in start/1 start(Input) -> case catch g(Input) of {exception1, Why} -> io:format("trouble is ~w ", [ Why ]); NormalReturnValue -> io:format("good input ~w ", [ NormalReturnValue ] ) end. 8> c(catch_it). {ok,catch_it} 9> catch_it:start(12). trouble is bad_number ok 10> catch_it:start(13). good input ok ok 27

  28. Processes Code is executed by a process A process keeps track of the program pointer, the stack, the variables values, etc. Every process has a unique process identifier: PID Processes are concurrent Virtual machine layer processes Preemptive multitasking Little overhead (e.g. 100.000 processes) Can use multiple CPUs on multiprocessor machines 28

  29. Concurrency • Several processes may use the same program code at the same time • each has own program counter, stack, and variables • programmer need not think about other processes updating the variables 29

  30. Message Passing • “!” is the send operator • Pid of the receiver is used as the address • Messages are sent asynchronously • The sender continues immediately • Any value can be sent as a message echo.erl -module(echo). -export([start/0,loop/0]). start() -> spawn(echo, loop, []). loop() -> receive {From, Message} -> io:format("> echo: ~w Msg: ~w ~n", [self(), Message]), From ! Message, loop() end. 1> c(echo). {ok,echo} 2> Id=echo:start(), 2> Id ! {self(),hello}. > echo: <0.38.0> Msg: hello {<0.31.0>,hello} 30

  31. Message Queues • Each process has a message queue (mailbox) • incoming messages are placed in the queue (no size limit) • A process receives a message when it extracts it from the mailbox • need not take the first message in the queue 31

  32. Receive a Message • receive-expressions are similar to case switches • patterns are used to match messages in the mailbox • messages in the queue are tested in order • only one message can be extracted each time • Selective receive • Patterns and guards permit message selection • receive-clauses are tried in order • If no message matches, the process suspends and waits for a new message 32

  33. Receive with Timeout • A receive-expression can have an after-part • can be an integer (milliseconds) or “infinity” • The process waits until a matching message arrives, or the timeout limit is exceeded • soft real-time: no guarantees sleep(T)- process suspends for T ms. sleep(T) -> receive after T -> true end. suspend() - process suspends indefinitely. suspend() -> receive after infinity -> true end. 33

  34. Send and Reply • Pids are often included in messages (self()), so that the receiver can reply to the sender • If the reply includes the Pid of the second process, it is easier for the first process to recognize the reply • Message order • The only guaranteed message order is when both the sender and the receiver are the same for both messages (first-in, first- out) • Selecting Unordered Messages • Using selective receive, it is possible to choose which messages to accept, even if they arrive in a different order 34

  35. Starting Processes • The “spawn” function creates a new process • The new process will run the specified function • The spawn operation always returns immediately • The return value is the Pid of the “child” 35

  36. Ping Pong tut16.erl -module(tut16). -export([start/0, ping/1, pong/0]). ping(0) -> pong ! finished, io:format("ping finished~n", []); ping(N) -> pong ! {ping, self()}, receive pong -> io:format("Ping received pong~n", []) end, ping(N - 1). pong() -> receive finished -> io:format("Pong finished~n", []); {ping, Ping_PID} -> io:format("Pong received ping~n", []), Ping_PID ! pong, pong() end. start() -> register(pong, spawn(tut16, pong, [])), spawn(tut16, ping, [3]). 1> c(tut16). {ok,tut16} 2> tut16:start(). Pong received ping <0.39.0> Ping received pong Pong received ping Ping received pong Pong received ping Ping received pong ping finished Pong finished 36

  37. Process Termination • Process Termination A process terminates when: • it finishes the function call that it started with • There is an exception that is not caught • All messages sent to a terminated process will be thrown away • Same Pid will not be used before long time 37

  38. Registered Processes • A process can be registered under a name • Any process can send a message to a registered process, or look up the Pid • The Pid might change (if the process is restarted and re-registered), but the name stays the same Pid = spawn(?MODULE, server, []), register(myserver, Pid), myserver ! Msg. 38

  39. Links and Exit Signals • Any two processes can be linked • Links are always bidirectionnal • When a process dies, an exit signal is sent to all linked processes, which are also killed • normal exit does not kill other processe • If a process sets its trap_exit flag, all signals will be caught and turned into normal messages • process_flag(trap_exit, true) • Signal is turned into message {‘EXIT’, Pid, ErrorTerm} • This way, a process can watch other processes 39

  40. Distribution • Running “erl” with the flag “-name xxx” • starts the Erlang network distribution system • Makes the virtual machine emulator a “node” (‘xxx@host.domain’) • Erlang nodes can communicate over the network (but must find each other first) • Possible to send a Pid from one node to another (Pids are unique across nodes) • You can send a message to any process through its Pid (even on another node) • You can run several Erlang nodes (with different names) on the same computer 40

  41. Connecting Nodes • Nodes are connected the first time they try to communicate • The function “net_adm:ping(Node)” is the easiest way to set up a connection between nodes • returns “pong” or “pang” • Send a message to a registered process using • “{Name,Node} ! Message” 41

  42. Running Remote Processes • Variants of the spawn function can start processes directly on another node • The module ‘global’ contains functions for • registering and using named processes over the whole network of connected nodes • setting global locks 42

  43. Ports:Talking to the Outside • Talks to an external (or linked-in) C program • A port is connected to the process that opened it • The port sends data to the process in messages • A process can send data to the port 43

  44. Conclusion • Try Erlang in practical session… • … and send me your own conclusion 44

  45. References • Special and greatful thanks to Franck Petit et Sebastien Tixeuil • http://www.erlang.org • http://en.wikibooks.org/wiki/Erlang_Programming • Use case pour normalien ;-) • Un Caml Light Distribué [Elkamel Merah , Allaoua Chaoui] • « … Pour l’extension concurrente de Caml Light nous proposons quelques primitives avec une sémantique très simple en utilisant le modèle du langage de programmation ERLANG. Le support de la création dynamique de processus et de la communication asynchrone entre processus sont les deux principales extensions du langage Caml Light. " 45

More Related