Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Similar documents
Introduction to Erlang. Franck Petit / Sebastien Tixeuil

An Introduction to Erlang

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

An Introduction to Erlang. Richard Carlsson

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

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

Principles of Programming Languages (E)

Starting the System & Basic Erlang Exercises

Programming Paradigms

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

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

Erlang Concepts. Programming for Beginners, Summer 2011

Erlang 101. Google Doc

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

Erlangen API Documentation

Erlang: distributed programming

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

MapReduce in Erlang. Tom Van Cutsem

COSC441. Lecture 8 Introduction to Erlang

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

An Erlang primer. Johan Montelius

3.1 Introduction. Computers perform operations concurrently

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

What s different about Factor?

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

Main Memory (Part I)

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Parallel Programming in Erlang (PFP Lecture 10) John Hughes

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

The PCAT Programming Language Reference Manual

Concurrent & Distributed Systems Supervision Exercises

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

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

The SPL Programming Language Reference Manual

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

Ch 9: Control flow. Sequencers. Jumps. Jumps

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Python in 10 (50) minutes

CSE 451 Midterm 1. Name:

SMD149 - Operating Systems

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

ERLANG BITS AND PIECES

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

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

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

Tasks. Task Implementation and management

Programming Language Concepts, cs2104 Lecture 04 ( )

Scheme as implemented by Racket

Pace University. Fundamental Concepts of CS121 1

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Lecture 8. Linda and Erlang

CS350: Final Exam Review

Operating Systems. Figure: Process States. 1 P a g e

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Processes, PCB, Context Switch

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Processes. Dr. Yingwu Zhu

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

Chapter 6 Control Flow. June 9, 2015

CS Introduction to Data Structures How to Parse Arithmetic Expressions

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS 221 Review. Mason Vail

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

C311 Lab #3 Representation Independence: Representation Independent Interpreters

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

DRAWING ENVIRONMENT DIAGRAMS

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Debugger Application (DEBUGGER) version 3.1

Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming.

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Erlang functional programming in a concurrent world

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

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

Compiler Application (COMPILER) version 4.4

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Programming for the Web with PHP

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Cliff Moon. Bottleneck Whack-A-Mole. Thursday, March 21, 13

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Chapters 9 & 10: Memory Management and Virtual Memory

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

Processes COMPSCI 386

Course: Operating Systems Instructor: M Umair. M Umair

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Computer architecture. A simplified model

Principles of Programming Languages 2017W, Functional Programming

Transcription:

Introduction to Erlang Franck Petit / Sebastien Tixeuil Firstname.Lastname@lip6.fr

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

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

Recursive Functions -module(mymath). -export([factorial/1]). factorial(0) -> 1; factorial(n) -> N * factorial(n-1). > mymath:factorial(6). 720

Tail Recursion The arity is part of the function name Non-exported functions are local to the module

Tail Recursion -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]

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

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

Numbers Regular numbers 123-34567 12.345-27.45e-05 #-notation for base-n integers 16#ffff $-notation for character codes (ISO-8859-1) $A (->65)

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

Tuples Terms separated by, and enclosed in {} {a,12, hello } {1,2,{3, 4},{a,{b,c}}} {} 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

Other Data Types Functions Binaries Process identifiers All terms are ordered and can be compared with <, >, ==, =:=, etc. References No separate booleans Erlang values in general are called terms

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

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

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

Fun Expressions Anonymous functions (lambda expressions) Can have several clauses All variables in the pattern are new All variable bindings in the fun are local Variables bound in the environment can be used in the fun-body

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

Pattern Matching mylength([]) -> 0; mylength([_ T]) -> mylength(t) + 1.

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

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).

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

List Processing 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) List Processing Functions member(x,l) append(l1,l2) reverse(l) delete_all(x,l)

Tuple Processing Tuple Processing BIFs tuple_to_list(t) element(n,t) setelement(n,t,val) size(l)... Multiple Return Values PID, now()...

Catching Exceptions throw: user defined error: runtime errors exit: end process only catch throw exceptions normally

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 Processes are concurrent

Processes: Implementation Virtual machine layer processes Preemptive multitasking Little overhead (e.g. 100.000 processes) Can use multiple CPUs on multiprocessor machines

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

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 -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. > Id=echo:start(), Id! {self(),hello}. echo: <0.35.0> Msg: hello {<0.32.0>,hello} >

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

Receiving 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

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

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, firstout)

Selecting Unordered Messages Using selective receive, it is possible to choose which messages to accept, even if they arrive in a different order

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

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

A Stateful Server The parameter variables of a server loop can be used to remember the current state

Hot Code Swapping When using module:function(...), the latest version of module is always used If the server module is recompiled and reloaded, the process will jump to the new code after handling the next message

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.

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 processes

Trapping Exit Signals If a process sets its trap_exit flag, all signals will be caught and turned into normal messages process_flag(trap_exit, true) { EXIT, Pid, ErrorTerm} This way, a process can watch other processes

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)

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

Distribution is Transparent 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

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

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