Introduction Erlang Conclusion - Stanford CS...

112
Introduction Erlang Conclusion CPL - Erlang Marco Patrignani K.U.Leuven 25 October 2013 Marco Patrignani CPL - Erlang 1/36

Transcript of Introduction Erlang Conclusion - Stanford CS...

Page 1: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

CPL - Erlang

Marco Patrignani

K.U.Leuven

25 October 2013

Marco Patrignani CPL - Erlang 1/36

Page 2: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Outline

1 IntroductionWhat, who, why, using Erlang?

2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

3 Conclusion

Marco Patrignani CPL - Erlang 2/36

Page 3: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Outline

1 IntroductionWhat, who, why, using Erlang?

2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

3 Conclusion

Marco Patrignani CPL - Erlang 3/36

Page 4: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

What is Erlang?

programming language + runtime system

Programming language:

functional (like Racket )dynamically typed (unlike

Java )concurrentfault-tolerant

Runtime system:

garbage collector (like

Java )

Marco Patrignani CPL - Erlang 4/36

Page 5: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

What is Erlang?

programming language + runtime system

Programming language:

functional (like Racket )dynamically typed (unlike

Java )concurrentfault-tolerant

Runtime system:

garbage collector (like

Java )

Marco Patrignani CPL - Erlang 4/36

Page 6: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

What is Erlang?

programming language + runtime system

Programming language:

functional (like Racket )

dynamically typed (unlike

Java )concurrentfault-tolerant

Runtime system:

garbage collector (like

Java )

Marco Patrignani CPL - Erlang 4/36

Page 7: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

What is Erlang?

programming language + runtime system

Programming language:

functional (like Racket )dynamically typed (unlike

Java )

concurrentfault-tolerant

Runtime system:

garbage collector (like

Java )

Marco Patrignani CPL - Erlang 4/36

Page 8: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

What is Erlang?

programming language + runtime system

Programming language:

functional (like Racket )dynamically typed (unlike

Java )concurrent

fault-tolerant

Runtime system:

garbage collector (like

Java )

Marco Patrignani CPL - Erlang 4/36

Page 9: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

What is Erlang?

programming language + runtime system

Programming language:

functional (like Racket )dynamically typed (unlike

Java )concurrentfault-tolerant

Runtime system:

garbage collector (like

Java )

Marco Patrignani CPL - Erlang 4/36

Page 10: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

What is Erlang?

programming language + runtime system

Programming language:

functional (like Racket )dynamically typed (unlike

Java )concurrentfault-tolerant

Runtime system:

garbage collector (like

Java )

Marco Patrignani CPL - Erlang 4/36

Page 11: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Why using Erlang?

inherently concurrent programs:internal support for processes

distributed programs on different machinesfault-tolerant systems:several mechanisms to recover faults without a system crashnon-stop applications:ability to load code at runtime

Marco Patrignani CPL - Erlang 5/36

Page 12: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Why using Erlang?

inherently concurrent programs:internal support for processesdistributed programs on different machines

fault-tolerant systems:several mechanisms to recover faults without a system crashnon-stop applications:ability to load code at runtime

Marco Patrignani CPL - Erlang 5/36

Page 13: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Why using Erlang?

inherently concurrent programs:internal support for processesdistributed programs on different machinesfault-tolerant systems:several mechanisms to recover faults without a system crash

non-stop applications:ability to load code at runtime

Marco Patrignani CPL - Erlang 5/36

Page 14: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Why using Erlang?

inherently concurrent programs:internal support for processesdistributed programs on different machinesfault-tolerant systems:several mechanisms to recover faults without a system crashnon-stop applications:ability to load code at runtime

Marco Patrignani CPL - Erlang 5/36

Page 15: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Why NOT using Erlang?

Poor support for frontends

Not as supported as other languages (unlike Java )Not known / understood as other languages (unlike

Java or C) (like Racket )

Marco Patrignani CPL - Erlang 6/36

Page 16: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Why NOT using Erlang?

Poor support for frontends

Not as supported as other languages (unlike Java )

Not known / understood as other languages (unlike

Java or C) (like Racket )

Marco Patrignani CPL - Erlang 6/36

Page 17: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Why NOT using Erlang?

Poor support for frontends

Not as supported as other languages (unlike Java )Not known / understood as other languages (unlike

Java or C) (like Racket )

Marco Patrignani CPL - Erlang 6/36

Page 18: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Who uses Erlang?

Marco Patrignani CPL - Erlang 7/36

Page 19: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

ConclusionWhat, who, why, using Erlang?

Who uses Erlang?

Marco Patrignani CPL - Erlang 7/36

Page 20: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Outline

1 IntroductionWhat, who, why, using Erlang?

2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

3 Conclusion

Marco Patrignani CPL - Erlang 8/36

Page 21: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

Marco Patrignani CPL - Erlang 9/36

Page 22: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

module definition

Marco Patrignani CPL - Erlang 9/36

Page 23: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app) .

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

EVERYTHING ends with a dot “.”

Marco Patrignani CPL - Erlang 9/36

Page 24: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num ,

Avg = average([Num , Local_Var]) ,

Sqr_Avg = math:sqrt(Avg) ,

io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

commas “,” separate things

Marco Patrignani CPL - Erlang 9/36

Page 25: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

list of exported functions

Marco Patrignani CPL - Erlang 9/36

Page 26: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export( [ func/1 ] ).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

list of exported functionslists are between square brackets “[ ] ”

Marco Patrignani CPL - Erlang 9/36

Page 27: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func /1 ]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

list of exported functionsarity of the function

Marco Patrignani CPL - Erlang 9/36

Page 28: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

function definition

Marco Patrignani CPL - Erlang 9/36

Page 29: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

function definition

LOCAL function definition

Marco Patrignani CPL - Erlang 9/36

Page 30: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

variable, single assignment

Marco Patrignani CPL - Erlang 9/36

Page 31: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok .

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

variable, single assignment

atom

Marco Patrignani CPL - Erlang 9/36

Page 32: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok .

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

variable, single assignment

atommind the difference!!

Marco Patrignani CPL - Erlang 9/36

Page 33: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average ([Num, Local_Var]),

Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

local function call

Marco Patrignani CPL - Erlang 9/36

Page 34: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average ([Num, Local_Var]),

Sqr_Avg = math:sqrt (Avg),

io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

local function callfunction call across modules

Marco Patrignani CPL - Erlang 9/36

Page 35: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average ([Num, Local_Var]),

Sqr_Avg = math : sqrt (Avg),

io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

local function callfunction call across modules

Module name – colon “:” – function name

Marco Patrignani CPL - Erlang 9/36

Page 36: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).

supports higher-order functions

(like Scala , Haskell , Racket )

Marco Patrignani CPL - Erlang 9/36

Page 37: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Functional code

-module(app).

-export([func/1]).

func( Num )->

Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),

ok.

average( L ) ->

lists:foldr( fun(El, Acc)-> El + Acc end , 0, L).

anonymous function (like Scala , Racket )

Marco Patrignani CPL - Erlang 9/36

Page 38: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang datatypes (selection of)

Integers: 1, 15 and -4217

Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.

Tuples: contain a fixed number data types:{E1, E2, ..., En}

Lists: [ Head | Tail ]. [] denotes an empty list.

Marco Patrignani CPL - Erlang 10/36

Page 39: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang datatypes (selection of)

Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"

Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.

Tuples: contain a fixed number data types:{E1, E2, ..., En}

Lists: [ Head | Tail ]. [] denotes an empty list.

Marco Patrignani CPL - Erlang 10/36

Page 40: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang datatypes (selection of)

Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished values

Pids: process identifierFuns: function closures created by expressions:fun(...) ->... end.

Tuples: contain a fixed number data types:{E1, E2, ..., En}

Lists: [ Head | Tail ]. [] denotes an empty list.

Marco Patrignani CPL - Erlang 10/36

Page 41: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang datatypes (selection of)

Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifier

Funs: function closures created by expressions:fun(...) ->... end.

Tuples: contain a fixed number data types:{E1, E2, ..., En}

Lists: [ Head | Tail ]. [] denotes an empty list.

Marco Patrignani CPL - Erlang 10/36

Page 42: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang datatypes (selection of)

Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.

Tuples: contain a fixed number data types:{E1, E2, ..., En}

Lists: [ Head | Tail ]. [] denotes an empty list.

Marco Patrignani CPL - Erlang 10/36

Page 43: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang datatypes (selection of)

Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.

Tuples: contain a fixed number data types:{E1, E2, ..., En}

Lists: [ Head | Tail ]. [] denotes an empty list.

Marco Patrignani CPL - Erlang 10/36

Page 44: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang datatypes (selection of)

Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.

Tuples: contain a fixed number data types:{E1, E2, ..., En}

Lists: [ Head | Tail ]. [] denotes an empty list.

Marco Patrignani CPL - Erlang 10/36

Page 45: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Dynamic typing

NO static typing (unlike Java )

This is a valid erlang program (will fail at runtime)

add( X, Y ) ->

X + Y

end.

...

add( 5, "marco" ).

Marco Patrignani CPL - Erlang 11/36

Page 46: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Dynamic typing

NO static typing (unlike Java )This is a valid erlang program (will fail at runtime)

add( X, Y ) ->

X + Y

end.

...

add( 5, "marco" ).

Marco Patrignani CPL - Erlang 11/36

Page 47: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Compiling and running Erlang code

Download and install the runtime:http://www.erlang.org/download.html

erl starts the consoleerlc filename.erl compiles

run commands within the consolec(filename). compiles from the console

Marco Patrignani CPL - Erlang 12/36

Page 48: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Compiling and running Erlang code

Download and install the runtime:http://www.erlang.org/download.html

erl starts the console

erlc filename.erl compiles

run commands within the consolec(filename). compiles from the console

Marco Patrignani CPL - Erlang 12/36

Page 49: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Compiling and running Erlang code

Download and install the runtime:http://www.erlang.org/download.html

erl starts the consoleerlc filename.erl compiles

run commands within the consolec(filename). compiles from the console

Marco Patrignani CPL - Erlang 12/36

Page 50: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Compiling and running Erlang code

Download and install the runtime:http://www.erlang.org/download.html

erl starts the consoleerlc filename.erl compiles

run commands within the console

c(filename). compiles from the console

Marco Patrignani CPL - Erlang 12/36

Page 51: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Compiling and running Erlang code

Download and install the runtime:http://www.erlang.org/download.html

erl starts the consoleerlc filename.erl compiles

run commands within the consolec(filename). compiles from the console

Marco Patrignani CPL - Erlang 12/36

Page 52: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Useful links

Erlang API: http://www.erlang.org/doc/man_index.htmlPart I of Concurrent programming in Erlang, by J. Armstrong,R. Virding, C. Wikström and M. Williams: http://www.erlang.org/download/erlang-book-part1.pdf

http:

//www.erlang.org/static/getting_started_quickly.html

http://www.erlang.org/doc/getting_started/users_

guide.html

http://learnyousomeerlang.com/

Marco Patrignani CPL - Erlang 13/36

Page 53: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Coding time

Coding timelength/1,dynamic type error with length/1, atom_to_list BIF,filter/2,anonymous functions, guards in functions,tailFilter/2

Marco Patrignani CPL - Erlang 14/36

Page 54: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Outline

1 IntroductionWhat, who, why, using Erlang?

2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

3 Conclusion

Marco Patrignani CPL - Erlang 15/36

Page 55: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The Actor model

everything is an actor

messages are the means of communication (asynchronous)actors have mailboxes where messages are queuedactors send and receive messages (only 2 primitives)

Marco Patrignani CPL - Erlang 16/36

Page 56: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The Actor model

everything is an actormessages are the means of communication (asynchronous)

actors have mailboxes where messages are queuedactors send and receive messages (only 2 primitives)

Marco Patrignani CPL - Erlang 16/36

Page 57: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The Actor model

everything is an actormessages are the means of communication (asynchronous)actors have mailboxes where messages are queued

actors send and receive messages (only 2 primitives)

Marco Patrignani CPL - Erlang 16/36

Page 58: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The Actor model

everything is an actormessages are the means of communication (asynchronous)actors have mailboxes where messages are queuedactors send and receive messages (only 2 primitives)

Marco Patrignani CPL - Erlang 16/36

Page 59: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Actors vs Threads

Actors

(generally) context switched bythe runtimemessage passing (asynchronous)no race conditons: no lockingcan deadlock

Threads

(generally) context switched bythe OSshared memory (sync/async)race conditions: needs lockingcan deadlock

Marco Patrignani CPL - Erlang 17/36

Page 60: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Actors vs Threads

Actors

(generally) context switched bythe runtimemessage passing (asynchronous)no race conditons: no lockingcan deadlock

Threads

(generally) context switched bythe OSshared memory (sync/async)race conditions: needs lockingcan deadlock

Marco Patrignani CPL - Erlang 17/36

Page 61: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang’s concurrency model, informally

Actor model:actors are lightweight processes, not OS processes, not threads

309 words of memory when spawned (very small!)fixed point context switches (optimal concurrency)processes have mailboxessend and receive are part of syntax, not in a library as

Scala , Akka (for Java ), actor framework (for .Net)

Marco Patrignani CPL - Erlang 18/36

Page 62: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang’s concurrency model, informally

Actor model:actors are lightweight processes, not OS processes, not threads309 words of memory when spawned (very small!)

fixed point context switches (optimal concurrency)processes have mailboxessend and receive are part of syntax, not in a library as

Scala , Akka (for Java ), actor framework (for .Net)

Marco Patrignani CPL - Erlang 18/36

Page 63: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang’s concurrency model, informally

Actor model:actors are lightweight processes, not OS processes, not threads309 words of memory when spawned (very small!)fixed point context switches (optimal concurrency)

processes have mailboxessend and receive are part of syntax, not in a library as

Scala , Akka (for Java ), actor framework (for .Net)

Marco Patrignani CPL - Erlang 18/36

Page 64: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang’s concurrency model, informally

Actor model:actors are lightweight processes, not OS processes, not threads309 words of memory when spawned (very small!)fixed point context switches (optimal concurrency)processes have mailboxes

send and receive are part of syntax, not in a library as

Scala , Akka (for Java ), actor framework (for .Net)

Marco Patrignani CPL - Erlang 18/36

Page 65: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang’s concurrency model, informally

Actor model:actors are lightweight processes, not OS processes, not threads309 words of memory when spawned (very small!)fixed point context switches (optimal concurrency)processes have mailboxessend and receive are part of syntax, not in a library as

Scala , Akka (for Java ), actor framework (for .Net)

Marco Patrignani CPL - Erlang 18/36

Page 66: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Receiving messages in Erlang

receive

mess ->

ok;{tuple, Number} ->

io:format("Received ~p",[Number])

after

10000 ->

ok

end.

Marco Patrignani CPL - Erlang 19/36

Page 67: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Receiving messages in Erlang

receive

mess ->

ok;

{tuple, Number} ->

io:format("Received ~p",[Number])

after

10000 ->

ok

end.

pattern matching (like Racket , Scala )

Marco Patrignani CPL - Erlang 19/36

Page 68: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Receiving messages in Erlang

receive

mess ->

ok ;

{tuple, Number} ->

io:format("Received ~p",[Number])

after

10000 ->

ok

end.

pattern matching (like Racket , Scala )separate by “;”

Marco Patrignani CPL - Erlang 19/36

Page 69: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Receiving messages in Erlang

receive

mess ->

ok;{tuple, Number} ->

io:format("Received ~p",[Number])

after

10000 ->

ok

end.

pattern matching (like Racket , Scala )

last clause ends with NOTHING

Marco Patrignani CPL - Erlang 19/36

Page 70: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Receiving messages in Erlang

receive

mess ->

ok;{tuple, Number} ->

io:format("Received ~p",[Number])

after

10000 ->

ok

end.

timeout clause

Marco Patrignani CPL - Erlang 19/36

Page 71: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The receive loop

a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive

server( NumMess )->

receive

mess ->

server( NumMess + 1 );

{tuple, Number} ->

server( Number ) + 1

after

10000 ->

ok

end.

Marco Patrignani CPL - Erlang 20/36

Page 72: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The receive loop

a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive

server( NumMess )->

receive

mess ->

server( NumMess + 1 );

{tuple, Number} ->

server( Number ) + 1

after

10000 ->

ok

end.

Marco Patrignani CPL - Erlang 20/36

Page 73: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The receive loop

a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive

server( NumMess )->

receive

mess ->

server( NumMess + 1 ) ;

{tuple, Number} ->

server( Number ) + 1

after

10000 ->

ok

end.

this one is ok

Marco Patrignani CPL - Erlang 20/36

Page 74: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The receive loop

a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive

server( NumMess )->

receive

mess ->

server( NumMess + 1 );

{tuple, Number} ->

server( Number ) + 1

after

10000 ->

ok

end.

this one is NOT

Marco Patrignani CPL - Erlang 20/36

Page 75: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The receive loop

a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive

server( NumMess )->

receive

mess ->

server( NumMess + 1 );

{tuple, Number} ->

server( Number ) + 1

after

10000 ->

ok

end.

State of the function (unlike Java , C)

Marco Patrignani CPL - Erlang 20/36

Page 76: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Sending messages in Erlang

messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}

PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2

(good for servers and for failing-respawning processes)local names are fetch with registered/0

Marco Patrignani CPL - Erlang 21/36

Page 77: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Sending messages in Erlang

messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}

PIDs are process identifiers

processes are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2

(good for servers and for failing-respawning processes)local names are fetch with registered/0

Marco Patrignani CPL - Erlang 21/36

Page 78: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Sending messages in Erlang

messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}

PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PID

the BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2

(good for servers and for failing-respawning processes)local names are fetch with registered/0

Marco Patrignani CPL - Erlang 21/36

Page 79: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Sending messages in Erlang

messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}

PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current process

PIDs can be registered to local names with register/2

(good for servers and for failing-respawning processes)local names are fetch with registered/0

Marco Patrignani CPL - Erlang 21/36

Page 80: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Sending messages in Erlang

messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}

PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2

(good for servers and for failing-respawning processes)

local names are fetch with registered/0

Marco Patrignani CPL - Erlang 21/36

Page 81: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Sending messages in Erlang

messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}

PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2

(good for servers and for failing-respawning processes)local names are fetch with registered/0

Marco Patrignani CPL - Erlang 21/36

Page 82: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Coding time

Coding timereceive messages, send messages, timeoutspawn/3, register/2, unregister/1

receive loop

Marco Patrignani CPL - Erlang 22/36

Page 83: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Outline

1 IntroductionWhat, who, why, using Erlang?

2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

3 Conclusion

Marco Patrignani CPL - Erlang 23/36

Page 84: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The “Let it crash” philosophy

expect failuredeal with it

Failures:in the same function: exceptions, errors and exits.

Handled with try / catch, like in Java and Scalain another processHandled as messages by monitor/2 and link/1

Marco Patrignani CPL - Erlang 24/36

Page 85: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The “Let it crash” philosophy

expect failuredeal with it

Failures:in the same function: exceptions, errors and exits.

Handled with try / catch, like in Java and Scala

in another processHandled as messages by monitor/2 and link/1

Marco Patrignani CPL - Erlang 24/36

Page 86: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

The “Let it crash” philosophy

expect failuredeal with it

Failures:in the same function: exceptions, errors and exits.

Handled with try / catch, like in Java and Scalain another processHandled as messages by monitor/2 and link/1

Marco Patrignani CPL - Erlang 24/36

Page 87: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Exceptions – throwing

Three types:

throw(Exception).

erlang:error(Reason).

exit(Reason).

Marco Patrignani CPL - Erlang 25/36

Page 88: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Exceptions – throwing

Three types:

throw(Exception).

erlang:error(Reason).

exit(Reason).

Marco Patrignani CPL - Erlang 25/36

Page 89: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Exceptions – throwing

Three types:

throw(Exception).

erlang:error(Reason).

exit(Reason).

Marco Patrignani CPL - Erlang 25/36

Page 90: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Exceptions – catching

throws(F) ->

try F() of_ -> ok

catch

Throw -> {throw, caught, Throw};

error:Error -> {error, caught, Error};

exit:Exit -> {exit, caught, Exit}

end.

Marco Patrignani CPL - Erlang 26/36

Page 91: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Monitoring

Unidirectionalreceive message when process dies:{ ’DOWN’, MonitorReference, process, Pid, Reason}

Pid = spawn( function ),

Ref = monitor(process, Pid).

... % or

{ Pid , Ref } = spawn_monitor( function ).

... %remove with

demonitor( Ref ).

Marco Patrignani CPL - Erlang 27/36

Page 92: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Monitoring

Unidirectionalreceive message when process dies:{ ’DOWN’, MonitorReference, process, Pid, Reason}

Pid = spawn( function ),

Ref = monitor(process, Pid).

... % or

{ Pid , Ref } = spawn_monitor( function ).

... %remove with

demonitor( Ref ).

Marco Patrignani CPL - Erlang 27/36

Page 93: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Linking

Bidirectionalreceive message when either process dies:{ ’EXIT’, Pid, Reason}only active after process_flag(trap_exit, true)

Pid = spawn( function ).

link( Pid ).

... % or

Pid = spawn_link( function ).

... %remove with

unlink( Pid ).

Marco Patrignani CPL - Erlang 28/36

Page 94: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Linking

Bidirectionalreceive message when either process dies:{ ’EXIT’, Pid, Reason}only active after process_flag(trap_exit, true)

Pid = spawn( function ).

link( Pid ).

... % or

Pid = spawn_link( function ).

... %remove with

unlink( Pid ).

Marco Patrignani CPL - Erlang 28/36

Page 95: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Coding time

Coding timetry/catchspawn_link/3, link/1, monitor/2

Marco Patrignani CPL - Erlang 29/36

Page 96: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Outline

1 IntroductionWhat, who, why, using Erlang?

2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

3 Conclusion

Marco Patrignani CPL - Erlang 30/36

Page 97: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

What is distribution?

processes on different Erlang nodes

different Erlang nodes on different machines

some applications are inherently distributed

Marco Patrignani CPL - Erlang 31/36

Page 98: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

What is distribution?

processes on different Erlang nodesdifferent Erlang nodes on different machines

some applications are inherently distributed

Marco Patrignani CPL - Erlang 31/36

Page 99: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

What is distribution?

processes on different Erlang nodesdifferent Erlang nodes on different machines

some applications are inherently distributed

Marco Patrignani CPL - Erlang 31/36

Page 100: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang Nodes

an Erlang node is an executing Erlang system

a node is given a name erl -name asd

the BIF node/0 returns the full namethe BIFs spawn/1-4, monitor/2, link/1, register/2 allwork also with node names

Marco Patrignani CPL - Erlang 32/36

Page 101: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang Nodes

an Erlang node is an executing Erlang systema node is given a name erl -name asd

the BIF node/0 returns the full namethe BIFs spawn/1-4, monitor/2, link/1, register/2 allwork also with node names

Marco Patrignani CPL - Erlang 32/36

Page 102: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang Nodes

an Erlang node is an executing Erlang systema node is given a name erl -name asd

the BIF node/0 returns the full name

the BIFs spawn/1-4, monitor/2, link/1, register/2 allwork also with node names

Marco Patrignani CPL - Erlang 32/36

Page 103: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Erlang Nodes

an Erlang node is an executing Erlang systema node is given a name erl -name asd

the BIF node/0 returns the full namethe BIFs spawn/1-4, monitor/2, link/1, register/2 allwork also with node names

Marco Patrignani CPL - Erlang 32/36

Page 104: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Nodes

connect with net_kernel:connect_node( NodeName )

net_kernel coordinates distributed Erlang systemsuse cookies to prevent communicationsuse -hidden to prevent communications

Marco Patrignani CPL - Erlang 33/36

Page 105: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Nodes

connect with net_kernel:connect_node( NodeName )

net_kernel coordinates distributed Erlang systems

use cookies to prevent communicationsuse -hidden to prevent communications

Marco Patrignani CPL - Erlang 33/36

Page 106: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Nodes

connect with net_kernel:connect_node( NodeName )

net_kernel coordinates distributed Erlang systemsuse cookies to prevent communications

use -hidden to prevent communications

Marco Patrignani CPL - Erlang 33/36

Page 107: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Nodes

connect with net_kernel:connect_node( NodeName )

net_kernel coordinates distributed Erlang systemsuse cookies to prevent communicationsuse -hidden to prevent communications

Marco Patrignani CPL - Erlang 33/36

Page 108: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang

Coding time

Coding timedistributed communication

Marco Patrignani CPL - Erlang 34/36

Page 109: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Conclusion

Erlang is:concurrent (also parallel and distributed)functionalfail-resistant

good for backend softwaregood for long-lived applications

Marco Patrignani CPL - Erlang 35/36

Page 110: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Conclusion

Erlang is:concurrent (also parallel and distributed)functionalfail-resistant

good for backend software

good for long-lived applications

Marco Patrignani CPL - Erlang 35/36

Page 111: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Conclusion

Erlang is:concurrent (also parallel and distributed)functionalfail-resistant

good for backend softwaregood for long-lived applications

Marco Patrignani CPL - Erlang 35/36

Page 112: Introduction Erlang Conclusion - Stanford CS Theorytheory.stanford.edu/~mp/mp/Teaching_files/2013-Erl.pdf · Introduction Erlang Conclusion Syntax and examples Concurrency in Erlang

IntroductionErlang

Conclusion

Homework and lab exercises

Find the homework exercise in Toledo.Lab sessions:

November 5, 2013 from 1:30 PM to 4:00 PM, Location:200A.00.124November 8, 2013 from 10:35 AM to 1:05 PM, Location:200A.SOL_Z

Marco Patrignani CPL - Erlang 36/36