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

Similar documents
Erlang functional programming in a concurrent world

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

Chordy: a distributed hash table. Johan Montelius and Vladimir Vlassov. August 29, 2016

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

An Erlang primer. Johan Montelius

Erlang: distributed programming

let s implement a memory Asynchronous vs Synchronous the functional way a first try

Indirect Communication

Producer Consumer in Ada

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

Erlang 101. Google Doc

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Concurre Concurr nt n Systems 8L for Part IB Handout 3 Dr. Steven Hand 1

KTH ROYAL INSTITUTE OF TECHNOLOGY. Remote Invocation. Vladimir Vlassov and Johan Montelius

concurrent programming XXL

Reminder from last time

Time and Space. Indirect communication. Time and space uncoupling. indirect communication

Distributed Systems. replication Johan Montelius ID2201. Distributed Systems ID2201

Parallel Programming in Erlang (PFP Lecture 10) John Hughes

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

Distributed file systems

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

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

Erlang. Types, Abstract Form & Core. Salvador Tamarit Muñoz. Universitat Politècnica de València

An Introduction to Erlang

Rudy: a small web server. Johan Montelius. October 2, 2016

Erlang. Joe Armstrong

Remote Invocation Vladimir Vlassov and Johan Montelius

Reminder from last ;me

The Go Programming Language. Frank Roberts

Namy: a distributed name server. Johan Montelius. October 2, 2016

An Introduction to Erlang

Message-passing concurrency in Erlang

Distributed Erlang and Map-Reduce

LFE - a lisp on the Erlang VM

Message Passing Part I

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

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

MapReduce in Erlang. Tom Van Cutsem

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Lecture 5: Erlang 1/31/12

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

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

Lecture 8. Linda and Erlang

A Small Web Server. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

Starting the System & Basic Erlang Exercises

Distributed Hash Tables

Inter-process communication (IPC)

Programming Paradigms

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

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

Distributed Object-Based Systems The WWW Architecture Web Services Handout 11 Part(a) EECS 591 Farnam Jahanian University of Michigan.

Erlang: concurrent programming. Johan Montelius. October 2, 2016

An Introduction to Erlang. Richard Carlsson

Functional Programming In Real Life

Message Passing. Advanced Operating Systems Tutorial 7

Robust Erlang (PFP Lecture 11) John Hughes

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Processes. Johan Montelius KTH

An Introduction to Erlang

A process. the stack

Detecting and Visualising Process Relationships in Erlang

Implementing languages on the Erlang VM

Erlang. Joe Armstrong.

Erlang Concepts. Programming for Beginners, Summer 2011

FRANCESCO CESARINI. presents ERLANG/OTP. Francesco Cesarini Erlang

Database Management Systems

April 2 to April 4, 2018

CS 415 Midterm Exam Spring 2002

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

CSE 153 Design of Operating Systems

Beautiful Concurrency with Erlang

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Taking the Derivative

Cloud Haskell Semantics (DRAFT)

3C05 - Advanced Software Engineering Thursday, April 29, 2004

CS 153 Design of Operating Systems Winter 2016

Programming with Math and Logic

Map-Reduce. John Hughes

CPS506 - Comparative Programming Languages Elixir

A heap, a stack, a bottle and a rack. Johan Montelius HT2017

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

Explicit Recursion in Generic Haskell

Two Phase Commit Protocol. Distributed Systems. Remote Procedure Calls (RPC) Network & Distributed Operating Systems. Network OS.

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

DISTRIBUTED SYSTEMS Principles and Paradigms Second Edition ANDREW S. TANENBAUM MAARTEN VAN STEEN. Chapter 1. Introduction

The case for reactive objects

F# for Industrial Applications Worth a Try?

CSCE 314 Programming Languages. Type System

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

Programming Language Impact on the Development of Distributed Systems

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

Simon Peyton Jones (Microsoft Research) Tokyo Haskell Users Group April 2010

Tackling Concurrency With STM. Mark Volkmann 10/22/09

Tackling Concurrency With STM

Programmazione di sistemi multicore

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

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

5.Language Definition

Transcription:

KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov Erlang Concurrent Oriented Programming processes have state communicate using message passing access and location transparent asynchronous Functional programming evaluation of expressions recursion data structures are immutable 2 History Today Developed at Ericsson in late eighties, early nineties. Targeting robust applications in the telecom world. Survived despite everything must be Java Growing interest from outside Ericsson. 3 4

Why Erlang? Erlang concurrency built-in multicore performance simple to implement fault tolerance scales well in distributed systems the functional subset concurrency distribution failure detection 5 6 Data structures Variables Literals atoms foo, bar,... numbers 123, 1.23.. bool true, false lexically scoped implicit scoping the procedure definition untyped assigned a value when introduced syntax X, Foo, BarZot, _anything Compound structures tuples {foo, 12, {bar, zot}} lists [], [1,2,foo,bar] 7 8

Assignment Pattern matching Assignment of values to variables is done by pattern matching <Pattern> = <Expression> A pattern can be a single variable Foo = 5 Bar = {foo, zot, 42} or a compound pattern {A, B} = {4, 5} {A, {B, C}} = {41, {foo, bar}} {A, {B, A}} = {41, {foo, 41}} Pattern matching Pattern matching is used to extract elements from a data structure. {person, Name, Age} = find_person(id, Employes), 9 10 Pattern matching No circular structures Pattern matching can fail {person, Name, Age} = {dog, pluto} You can not construct circular data structures in Erlang. (a structure in which the last element is a pointer to the first) Pros makes the implementation easier. Cons Someone might like/need circular structures. 11 12

Definitions if statement area(x, Y) -> X * Y. fac(n) -> if N == 0 -> 1; N > 0 -> N*fac(N-1) 13 14 case statement case statement sum(l) -> case L of [] -> 0; [H T] -> H + sum(t) member(x,l) -> case L of [] -> no; [X _] -> yes; [_ T] -> member(x, T) 15 16

Higher order Higher order F = fun(x) -> X + 1 F(5) map(fun, List) -> case List of [] -> []; [H T] -> [Fun(H) map(fun, T)] 17 18 Modules Modules -module(lst). -export([reverse/1]). reverse(l) -> reverse(l,[]). reverse(l, A) -> case L of [] -> A; [H T] -> reverse(t,[h A]) -module(test). -export([palindrome/1]). palindrome(x) -> case lstreverse(x) of X -> yes; _ -> no 19 20

Concurrency Spawn a process Concurrency is explicitly controlled by creation (spawning) of processes. A process is when created, given a function to evaluate. no one cares about the result Sending and receiving messages is the only way to communicate with a process. no shared state (...well, almost) -module(account) start(balance) -> spawn(fun() -> server(balance) end). server(balance) -> 21 22 Receiving a message Sending a message server(balance) -> receive {deposit, X} -> server(balance+x); {withdraw, X} -> server(balance-x); quit -> ok Account = accountstart(40), Account! {deposit, 100}, Account! {withdraw, 50}, 23 24

RPC-like communication RPC-like communication server(balance) -> receive {check, Client} -> Client! {saldo, Balance}, server(balance); friday(account) -> Account! {check, self()}, receive {saldo, Balance} -> if Balance > 100 -> party(account); true -> work(account) end 25 26 Implicit deferral Registration A process will have an ordered sequence of received messages. A node registers associate names to process identifiers. The first message that matches one of several program defined patterns will be delivered. Pros and cons one can select which messages to handle first risk of forgetting messages that are left in a growing queue register(alarm_process, Pid) Knowing the registered name of a process you can look-up the process identifier. The register is a shared data structure! 27 28

Registration Starting a node Erlang nodes (an Erlang virtual machine) can be connected in a group. Each node has a unique name. Processes in one node can send messages to and receive messages from processes in other nodes using the same language constructs moon> erl -sname gold -setcookie xxxx (gold@moon)> 29 30 Failure detection Monitor a process can monitor another process if the process dies a messages is placed in the message queue Ref = erlangmonitor(process, Account), Account! {check, self()}, the message will indicate if the termination was normal or abnormal or... if the communication was lost receive {saldo, Balance} -> { DOWN, Ref, process, Account, Reason}-> end 31 32

Atomic termination A process can link to another process, if the process dies with an exception the linked process will die with the same exception. Processes that depend on each other are often linked together, if one dies they all die. Linking P = spawn_link(fun()-> server(balance) end), do_something(p), 33 34 Summary functional programming processes message passing distribution monitor/linking 35