Erlang. An introduction. Paolo Baldan Linguaggi e Modelli per il Global Computing AA 2016/2017

Similar documents
Programming Paradigms

An Introduction to Erlang

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Erlang 101. Google Doc

Erlang. Joe Armstrong.

Erlang. Joe Armstrong

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part

Starting the System & Basic Erlang Exercises

Erlang: distributed programming

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

An Introduction to Erlang. Richard Carlsson

Programming Paradigms

An Introduction to Erlang

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

DISTRIBUTED SYSTEMS [COMP9243] Lecture 1.5: Erlang INTRODUCTION TO ERLANG BASICS: SEQUENTIAL PROGRAMMING 2. Slide 1

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

Message-passing concurrency in Erlang

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

An Introduction to Erlang

Scenes From the Language Struggle in Toronto, Ontario. Mike Nolta

LFE - a lisp on the Erlang VM

Let s Get Functional. Learn You Some Erlang for Great Good! 2013, Fred Hébert

The Actor Model applied to the Raspberry Pi and the Embedded Domain. The Erlang Embedded Project. Omer

Advanced Functional Programming, 1DL Lecture 2, Cons T Åhs

Getting Started with Erlang. version 5.6

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

FRANCESCO CESARINI. presents ERLANG/OTP. Francesco Cesarini Erlang

Programming Safe Agents in Blueprint. Alex Muscar University of Craiova

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

An Introduction to Erlang

All you need is fun. Cons T Åhs Keeper of The Code

COSC441. Lecture 8 Introduction to Erlang

Erlang and Concurrency. André Pang Rising Sun Research

Reminder from last time

Message Passing. Advanced Operating Systems Tutorial 7

Erlang functional programming in a concurrent world

Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov

MapReduce in Erlang. Tom Van Cutsem

The Actor Model applied to the Raspberry Pi and the Embedded Domain. Omer

Clojure. A (not-so-pure) functional approach to concurrency. Paolo Baldan Linguaggi per il Global Computing AA 2016/2017

Shared state model. April 3, / 29

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

COP4020 Programming Assignment 1 - Spring 2011

Debugger Copyright Ericsson AB. All Rights Reserved. Debugger September 24, 2018

CPS 506 Comparative Programming Languages. Programming Language Paradigm

concurrent programming XXL

Erlang Concepts. Programming for Beginners, Summer 2011

Erlang - functional programming in a concurrent world. Johan Montelius KTH

Experiments in OTP-Compliant Dataflow Programming

Type Checking and Type Equality

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 12 09/29/2016

An Introduction to Erlang

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

CPS506 - Comparative Programming Languages Elixir

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Beautiful Concurrency with Erlang

Robust Erlang (PFP Lecture 11) John Hughes

A Language for Specifying Type Contracts in Erlang and its Interaction with Success Typings

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

Programming Languages

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Debugger Application (DEBUGGER) version 3.1

Programming Languages

Lecture 9. Part I. Overview of Message Passing. Communication Coupling. Decoupling Blackboard. Decoupling Broadcast. Linda and Erlang.

Chapter 15. Functional Programming Languages

Organized by Jean-François Cloutier Held at Big Room Studios, Portland, ME. Holy Elixir, Batman! Meetup June 16, 2015

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

LECTURE 16. Functional Programming

Functional Programming and Haskell

Metaprogramming and symbolic execution for detecting runtime errors in Erlang programs

The Go Programming Language. Frank Roberts

Programming Languages and Techniques (CIS120)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

The do s and don ts of error handling. Joe Armstrong

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Concepts of programming languages

Lecture 5: Erlang 1/31/12

Detecting and Visualising Process Relationships in Erlang

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Begin at the beginning

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Functional Programming In Real Life

Java+- Language Reference Manual

Luerl - an implementation of Lua on the Erlang VM

Hands-On Lab. Introduction to F# Lab version: Last updated: 12/10/2010. Page 1

Implementing Symmetric Multiprocessing in LispWorks

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Concurrent & Distributed Systems Supervision Exercises

Concurrency: what, why, how

Erlang: An Overview. Part 1 Sequential Erlang. Thanks to Richard Carlsson for the original version of many slides in this part

Producer Consumer in Ada

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Implementing languages on the Erlang VM

Lecture 8. Linda and Erlang

<Urban.Boquist. com> Rev PA

Computer Components. Software{ User Programs. Operating System. Hardware

CGS 3066: Spring 2015 JavaScript Reference

Perl Library Functions

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Transcription:

Erlang An introduction Paolo Baldan Linguaggi e Modelli per il Global Computing AA 2016/2017

Erlang, in a slogan Declarative (functional) language for concurrent and distributed systems Erlang = Functions + Concurrency + Messages

Where does it come from? Old language with modern design Created in 86 at Ericsson Open sourced in 98 Programming with Erlang published in 07 Getting more and more popular

Intended domain Highly concurrent (hundreds of thousands of parallel activities) (Soft) real time Distributed (transparently) High Availability (down times of minutes/year never down) Complex software (million of lines of code) Continuous operation (years) Continuous evolution / In service upgrade

Principles Concurrent programming Functional programming Concurrency Oriented programming Erlang Fault tolerance Multicore

Basics Dynamic typing Light-weight processes Total separation between processes (no sharing, naturally enforced by functional style) (Fast) Message passing Transparent distribution

Multicore (& co.) era Paradigm shift in CPU architecture Multi core (easily up to 8 cores) GPU - Graphical Processing Unit NOC - Network on chip (up to 80 and more cores)

Hope Language and programming style exploiting parallelism Ideally: Make my program run N times faster on an N core CPU with no changes to the program no pain and suffering Can we do this? Somehow

Fault tolerance To make a system fault tolerant you need at least two computers (and some form of coordination) C1 C2 If one crashes, the other takes over

Fault tolerance To make a system very fault tolerant you need (at least) many computers C1 C2 C3 C4 C5 Cn Which also addresses scalability

Concurrency Distribution Fault tolerance Scalability are faces of the same medal (inseparable)

Models of concurrency Shared memory Threads Mutexes and locks Message passing Processes Messages

Shared Memory Problems: What if a thread fails in the critical section? Corrupted shared memory Where do we (physically) locate the shared memory for distributed systems?

Shared Memory Sharing memory threats fault tolerance thread safety

Message passing Concurrency No sharing (share by communicating) Pure message passing No locks, mutexes etc Lots of processes (fault tolerant, scalable)

Message Passing Concurrency Machine/node Process Message

Concurrency Oriented Programming A style of programming where concurrency is used to structure the application Large numbers of processes Complete isolation of processes / No sharing of data Pure message passing Location transparency (with named processes)

Message from My first message is that concurrency is best regarded as a program structuring principle Sir Tony Hoare Structured Oriented Programming

COP does it better We intuitively understand concurrency The world is parallel The world is distributed Making a real-world application is based on observation of the concurrency patterns and message channels in the application Easy to make scalable, distributed application

Functional programming No mutable state copy, not modify essentially no side effects Nothing to lock and automatic thread safety when parallellized The same function called twice on the same argument will return the same results Compositionality!

Get into the fight

Erlang Functional dynamic(ally typed) garbage collected eager compiled to Erlang runtime

Shell with an interpreter Can play most tricks in the shell! baldan@comp:~/erlang$ erl Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp: 8:8] [async-threads:10] [hipe] [kernel-poll:false] Eshell V7.2.1 (abort with ^G) 1>...

Expression Terminated with a period evaluate to a a value 1> 2 + 15. 17 2> 15 div 2. 7 3> 2#101010. 42 4> 16#AE. 174

Variables Start with Capital letter Once assigned, a variable is immutable = is pattern matching Compare (and possibly instantiate vars in the lhs) 1> Two = 2. 2 2> Two = 2. 2 3> Two = 3. ** exception error: no match of right hand side value 3 4>

Modules Programs are organised in modules -module(mymath). -export([fac/1]). fac(0) -> 1; fac(n) -> N * fac(n-1).

Modules Some functions are exported, some others are not -module(mymath). -export([fac/1]). add(x,0) -> X; add(x,y) -> add(x,y-1)+1. mul(x,0) -> 0; mul(x,y) -> add(mul(x,y-1),x). fac(0) -> 1; fac(n) -> mul(n, fac(n-1)).

Compilation A module can be compiled 1> c(mymath). {ok,mymath} And used 2> mymath:fac(5). 24 25> mymath:fac(7). 5024

Besides integers Atoms: constants with their own name for value 1> atom. atom 2> new_atom. new_atom Booleans 1> true and false. false 2> false or true. true 3> true xor false. true 4> not false. true 5> not (true and true). false

1> 5 =:= 5. true 2> 1 =:= 0. false 3> 1 =/= 0. true 4> 5 =:= 5.0. false 5> 5 == 5.0. true 6> 5 /= 5.0. false Comparisons

Type checking, sometimes 1> 5 == true. false 12> 5 + paperino. ** exception error: bad argument in an arithmetic expression in operator +/2 called as 5 + llama 14> 0 == false. false 15> 1 < false. true What the comparison hell?

Type checking, sometimes (cont d) You can compare everything to everything A total order is fixed by convention number < atom < reference < fun < port < pid < tuple < list < bit string

Tuples Syntax {comp1, comp2, comp3} 1> X = 10, Y = 4. 4 2> Point = {X,Y}. {10,4} 3> {First,_} = Point. {10,4} 4> First. 10.

Tagged Tuples Tuples can be tagged for identifying their structure 1> P = {point,{10,5}}. {point,{10,5}} 2> CP = {colpoint, {{10,5},red}}. {colpoint, {{10,5},red}} 3> {colpoint, Val} = P ** exception error: no match of right hand side value {point,{10,5}} 4> {colpoint, Val} = CP {colpoint,{{10,5},red} 5> Val. {{10,5},red}

Temperature converter Temperatures denoted by values {Unit, Value} where Unit can be c(elsius), or f(ahrenheit) -module(conv). -export([convert/1]). convert({c, X}) -> {f, 1.8 * X + 32}; convert({f, X}) -> {c, (X-32)/ 1.8}.

Temperature converter 2> conv:convert({f,100}). {c,37.8} 3> conv:convert({c,100}). {f,212.0} 4> conv:convert({k,2}). ** exception error: no function clause matching )

Lists Syntax [elem1, elem2, elem3,.] Any type of element 1> [1, 2, 3, {numbers,[4,5,6]}, 5.34, atom]. [1,2,3,{numbers,[4,5,6]},5.34,atom] Head and tail 11> hd([1,2,3,4]). 1 12> tl([1,2,3,4]). [2,3,4]

Head/Tail, with matching 15> [ Head Tail ] = [1,2,3,4]. [1,2,3,4] 16> Head. 1 17> Tail. [2,3,4] 18> [NewHead NewTail] = Tail. [2,3,4] 19> NewHead. 2

Length len([]) -> 0; len([_ T]) -> 1+len(T). With tail recursion lentr(l) -> lentr(l,0). lentr([],n) -> N; lentr([_ T],N) -> lentr(t,n+1).

Strings Strings are lists of unicode characters When sent to output Erlang guess that the string, rather than the list should be printed. 4> [115,116,114,105,110,103]. "string"

More list ops Concatenation, subtraction 5> [1,2,3] ++ [4,5]. [1,2,3,4,5] 6> [1,2,3,4,5] -- [1,2,3]. [4,5] 7> [2,4,2] -- [2,4]. [2] 8> [2,4,2] -- [2,4,2]. []

Comprehension Doubling 1> [2*N N <- [1,2,3,4]]. [2,4,6,8] Get the even 2> [X X <- [1,2,3,4,5,6,7,8,9,10], X rem 2 =:= 0]. [2,4,6,8,10] Sum 5> [X+Y X <- [1,2], Y <- [2,3]]. [3,4,4,5]

Quicksort -module(quicksort). -export([qsort/1, triqsort/1]). qsort([]) -> []; qsort([pivot Rest]) -> qsort([ X X <- Rest, X < Pivot]) ++ [Pivot] ++ qsort([ Y Y <- Rest, Y >= Pivot]).

Output to the terminal Formatted output goes like this 31> io:format("hello world~n", []). hello world ok 32> io:format("one term: ~w~n", [hello]). one term: hello ok 33> io:format("two terms: ~w ~w~n", [hello, world]). two terms: hello world ok

Pattern matching, again From. function greet(gender,name) if Gender == male then print("hello, Mr. %s!", Name) else if Gender == female then print("hello, Mrs. %s!", Name) else print("hello, %s!", Name) end

Pattern matching to greet(male, Name) -> io:format("hello, Mr. ~s!", [Name]); greet(female, Name) -> io:format("hello, Mrs. ~s!", [Name]); greet(_, Name) -> io:format("hello, ~s!", [Name]).

Pattern matching second([_,x _]) -> X. Bound and unbound vars same(x,x) -> true; same(_,_) -> false.

If Sugar for (conditional) pattern matching test(x,y) -> if X < Y -> -1; X == Y -> 0; X =/= Y -> 1 end. test(x,y) when X < Y -> -1; test(x,x) -> 0; test(x,y) when X > Y -> 1.

Case Sugar for (conditional) pattern matching insert(x,set) -> case lists:member(x,set) of true -> Set; false -> [X Set] end.

Types? Dynamically typed Types inferred runtime (type errors are possible ) Type test functions is_atom/1, is_binary/1,is_bitstring/1, is_boolean/1 Type conversion functions atom_to_list/1, list_to_atom/1, integer_to_list/1

Higher-order Functions are first class values 1> Double = fun(x) -> X * 2 end. #Fun<erl_eval.6.54118792> 2> Double(3). 6. Profitably used as function arguments map(fun, [First Rest]) -> [Fun(First) map(fun,rest)]; map(fun, []) -> [].

Map, filter Apply to all elements of a list map(fun, [First Rest]) -> [Fun(First) map(fun,rest)]; map(fun, []) -> []. Filter only elements satisfying a predicate filter(pred, L) -> lists:reverse(filter(pred, L,[])). filter(_, [], Acc) -> Acc; filter(pred, [H T], Acc) -> case Pred(H) of true -> filter(pred, T, [H Acc]); false -> filter(pred, T, Acc) end.

Example Convert a list of temperatures 1> Temps = [{ Milan, {c,10}}, { Turin, {c,12}}, ] 2> Fun = fun(x) -> {City,Temp} = X, {City,conv:convert(Temp)} end. 3> map(fun, Temps) 4> [{"Milan",{f,50.0}},{"Turin",{f,53.6}}]

Example Keep only warm temperatures 1> Temps = [{ Milan, {c,10}}, { Turin, {c,12}}, ] 2> Pred = fun(x) -> {City,{c,Temp}} = X, Temp >= 12 end. 3> filter(fun, Temps) 4> [{ Turin, {c,12}}, ]

Concurrency

Processes Basic structuring concept for concurrency (everything is a process) Execute a function on some parameters Strongly isolated (no sharing) Identified by an identifier (Id or name), that can be passed (and cannot be forged)

Messages Processes communicate through asynchronous message passing (with known companions) They are atomic (delivered or not) Messages are kept in a message queue A process can detect failure in another process

Actor model Everything is an actor and actors execute concurrently Actors can send messages to other actors, asynchronously (mailing); create new actors; designate the behaviour for the messages received An actor can communicate only with actors whose address is known, and addresses can be passed

Syntax spawn(module, Exported_Function, Arg List) Create a new process executing a function exported by some module on a list of arguments Returns a pid, uniquely identifying the process

Tick tock -module(proc1). -export([start/0, tick/2]). tick(msg, 0) -> done; tick(msg, N) -> io:format( Here is tick saying \"~p\" ~B times~n, [Msg,N]), tick(msg, N - 1). start() -> spawn(proc1, tick, [tick, 3]), spawn(proc1, tick, [tock, 2]).

Tick tock run 7> proc:start(). Here is tick saying "tick" 3 times Here is tick saying "tock" 2 times Here is tick saying "tick" 2 times Here is tick saying "tock" 1 times Here is tick saying "tick" 1 times 8>

Fast spawning Lightweight (not 1-1 with system threads)

Communication Asynchronous message passing Messages are valid Erlang terms (lists, tuples, integers, atoms, pids, ) Each process has a message queue A message can be sent to a process (not blocking) A process can selectively receive messages on its queue (blocking)

Send and receive Send pid! msg Receive receive msg1 -> action1; msg2 -> action2; end

Multiplier: server -module(proc2). -export([start/0, mul_server/0]). mul_server() -> receive {X, Y, Pid} -> Pid! X*Y, mul_server(); stop -> io:format("server stopping... ", []) end.

Multiplier: client start() -> Server = spawn(proc2, mul_server, []), Server! {2, 2, self()}, Server! {2, 4, self()}, receive P1 -> io:format("product 2*2 = ~B~n, [P1]) end, receive P2 -> io:format("product 2*4 = ~B~n, [P2]) end, Server!stop.

Registering Processes can be registered register(pid, Alias) Alias visible to the node (useful for restarting behaviours) Alias can be unregistered (done automatically when aliased process dies) unregister(alias)

Timing out A receive can be exited after some time: receive Msg1 -> action1; Msg2 -> action2 after Time -> action after timeout Example

Multiplier, again The server (mul_server) is registered (multiplier) Accessible to clients knowing the name The server can be stopped only by the creator (secret = creator pid not very secret) The client gets tagged messages and possibly timeouts if answer takes too long. proc3.erl

Robustness Primitives allows to connect processes in a way that processes in the same group are notified of abnormal (error) events The structuring can be hierarchical allowing for layered applications workers monitors supervisors

Links and monitors A process can exit normally run out of code or exit(normal) abnormally error or exit(reason) A process can be linked to or monitor another process

Links (Bidirectional) link between caller and pid link(pid) When a process exits, linked processes receive a signal, carrying pid and exit reasons By default normal exits are ignored, abnormal exits Kill the receiving process. Propagate the error signal to the links of the killed process

Links, more control A process can become a supervisor process process_flag(trap_exit, true). The exit signal is caught as a message { EXIT',Pid, Reason} E.g., in the process start (see before) link(server), receive { EXIT, Server, Reason} -> depending on reason, possibly restart the server

Example A server that gets a function with a list of arguments Execute the function as a monitored child process, keeping a list of the unfinished tasks Get the result and provides it to the client. In case of abnormal exit message, retry

Monitors Create a unidirectional link: current process monitors the process Pid monitor(process, Pid) On exits the monitor process gets a message {'DOWN', MonitorReference, process, Pid, Reason}

Distribution Just move to a different scale Concept of a node Most primitives on intra-node processes have a form acting on inter-node processes Possibility of checking if a node is alive (reachable)

Concluding Concurrency Oriented Programming (~ actor model) Distributed Programming Soft Real-time Environment Continuous Operation (hot-swapping) Open Telephone Platform (OTP), framework for distributed applications, with pre-designed skeletons and patterns

Not perfect (as everything in the world) Alternatives Elixir, Clojure, Primitive security model (restricting access to a node / process capabilities) Message passing only is good, but can be heavy when supporting the sharing of large data sets