Notes on Erlang and Actors

 


A few Notes on Erlang and Actors

Erlang is a functional languages containing also primitives which implement the Actors Model of Concurrency.

The Actors Model of concurrency is sometimes also referred to as Agents Model.
As a matter of fact an Agent can be roughly looked at as an Actor with a few extra capabilities (it has goals to reach and strategies and tools enabling it to reach them; sometimes it has also mobility capabilities).

Let us briefly recall the Actors Model of Concurrency (notes by Sander Sonajalg).

  • An actor is a concurrent computation unit that does not share any resources with other actors
  • Communication is implemented by actors sending each-other messages
  • Actors are computational agents which map each incoming communication to a triple consisting of:
    • A finite set of communications sent to other actors
    • A new behavior (which will govern the response to the next communication processed)
    • A finite set of new actors created
  • Rather than explicitly “sleeping” or “waking up”, actors “react” to the “events”
    • “events” = interactions with other actors = messages
  • Actors get passive when they've finished their previous tasks and nobody has sent them new messages of interest
  • Passive actors activate immediately when somebody has sent them an interesting message
  • Message sending is asynchronous
  • Each actor has a “mailbox” for storing messages that are not consumed immediately.
  • If message arrives when actor is busy working on a previous message, it gets stored in it's mailbox
  • If actor arrives at a point where it waits new messages to continue, it first looks through it's mailbox.
    • Messages of unsuitable type are ignored
    • First suitable message allows actor to continue


The Actors Model of Concurrency requires no sharing of resources. This is why a functional language is best suited for implementing it.

The Actor Model requires actors to possibly possess a state.
State in Erlang is realized by means of extra arguments in (recursive) funtion calls (like when we define a tail-recursive version of a proper recursive function using extra arguments [see exercises page])

Erlang is an untyped language. Better, it is a dinamically typed language.
To be dinamically typed does not means that types do not exists, but simply that they are checked at run-time.

Besides base types (boolean, integers, etc.) Erlang can structure values of various types using tuples and lists.

Erlang tuples are like PICT tuples (with '{' and '}' instead of '[' and ']')

List are denoted using a sintax similar to the one of PROLOG: [] is the empty list, [X|XS] is the list having X as first element and XS as rest of the list.

Tuples can be used, like in PICT, to form patterns. Patterns are tipically formed using values (among which atoms), variables, and the tuple constructor.

In Erlang pattern mathing can be used like in Haskell, in function definitions, or like in PICT, for input operations (receive).

Function definitions are very similar to Haskell, apart from sintactical differences.

A basic type in Erlang is the one of Atoms. Erlang atoms are exactly like the atoms in PROLOG: an atom is a name having itself as value. Atoms are used in tuples usually in order to denote what sort of message we are sending/receiving. In Erlang, atoms are identifiers beginning with a lower-case letter, whereas variables are identifiers beginning with an Upper-case letter. It is possible to associate a value to a variable by means of the '=' operator (directly or by means of pattern matching).

Besides built-in types, in Erlang it is also possible to define new types and provide type specifications for functions.
Type informations can be used to document function interfaces, provide more information for bug detection tools such as Dialyzer, and can be exploited by documentation tools such as Edoc for generating program documentation of various forms. [Erlang EEP8]

As in PROLOG, Erlang is provided with many BIF (Built In Functions) enabling the programmer to check inside the program the types of the values used.

So Erlang uses a completely different approach to types w.r.t. Haskell and static typing in general.

Erlang is a language with a call-by-value evaluation strategy.