Erlang (programming language)


Erlang is a general-purpose, concurrent, functional programming language, and a garbage-collected runtime system. The term Erlang is used interchangeably with Erlang/OTP, or Open Telecom Platform, which consists of the Erlang runtime system, several ready-to-use components mainly written in Erlang, and a set of design principles for Erlang programs.
The Erlang runtime system is designed for systems with these traits:
The Erlang programming language has immutable data, pattern matching, and functional programming. The sequential subset of the Erlang language supports eager evaluation, single assignment, and dynamic typing.
It was originally proprietary software within Ericsson, developed by Joe Armstrong, Robert Virding, and Mike Williams in 1986, but was released as free and open-source software in 1998. Erlang/OTP is supported and maintained by the Open Telecom Platform product unit at Ericsson.

History

The name Erlang, attributed to Bjarne Däcker, has been presumed by those working on the telephony switches to be a reference to Danish mathematician and engineer Agner Krarup Erlang and a syllabic abbreviation of "Ericsson Language". Erlang was designed with the aim of improving the development of telephony applications. The initial version of Erlang was implemented in Prolog and was influenced by the programming language PLEX used in earlier Ericsson exchanges. By 1988 Erlang had proven that it was suitable for prototyping telephone exchanges, but the Prolog interpreter was far too slow. One group within Ericsson estimated that it would need to be 40 times faster to be suitable for production use. In 1992, work began on the BEAM virtual machine which compiles Erlang to C using a mix of natively compiled code and threaded code to strike a balance between performance and disk space. According to Armstrong, the language went from lab product to real applications following the collapse of the next-generation AXE telephone exchange named :sv:AXE-N|AXE-N in 1995. As a result, Erlang was chosen for the next asynchronous transfer mode exchange AXD.
In 1998 Ericsson announced the AXD301 switch, containing over a million lines of Erlang and reported to achieve a high availability of nine "9"s. Shortly thereafter, Ericsson Radio Systems banned the in-house use of Erlang for new products, citing a preference for non-proprietary languages. The ban caused Armstrong and others to leave Ericsson. The implementation was open-sourced at the end of the year. Ericsson eventually lifted the ban and re-hired Armstrong in 2004.
In 2006, native symmetric multiprocessing support was added to the runtime system and VM.

Processes

Joe Armstrong, co-inventor of Erlang, summarized the principles of processes in his PhD thesis:
Joe Armstrong remarked in an interview with Rackspace in 2013: "If Java is 'write once, run anywhere', then Erlang is 'write once, run forever'.”

Usage

In 2014, Ericsson reported Erlang was being used in its support nodes, and in GPRS, 3G and LTE mobile networks worldwide and also by Nortel and T-Mobile.
As Tim Bray, director of Web Technologies at Sun Microsystems, expressed in his keynote at O'Reilly Open Source Convention in July 2008:
Erlang is the programming language used to code WhatsApp.
Since being released as open source, Erlang has been spreading beyond Telecoms, establishing itself in other verticals such as FinTech, Gaming, Healthcare, Automotive, IoT and Blockchain. Apart from WhatsApp there are other companies listed as Erlang’s success stories: Vocalink, Goldman Sachs, Nintendo, AdRoll, Grindr, BT Mobile, Samsung, OpenX, SITA.

Functional programming examples

Factorial

A factorial algorithm implemented in Erlang:

-module. % This is the file 'fact.erl', the module and the filename must match
-export. % This exports the function 'fac' of arity 1
fac -> 1; % If 0, then return 1, otherwise
fac when N > 0, is_integer -> N * fac.
% Recursively determine, then return the result
%
%% This function will crash if anything other than a nonnegative integer is given.
%% It illustrates the "Let it crash" philosophy of Erlang.

Fibonacci sequence

A tail recursive algorithm that produces the Fibonacci sequence:

%% The module declaration must match the file name "series.erl"
-module.
%% The export statement contains a list of all those functions that form
%% the module's public API. In this case, this module exposes a single
%% function called fib that takes 1 argument
%% The general syntax for -export is a list containing the name and
%% arity of each public function
-export.
%% ---------------------------------------------------------------------
%% Public API
%% ---------------------------------------------------------------------
%% Handle cases in which fib/1 receives specific values
%% The order in which these function signatures are declared is a vital
%% part of this module's functionality
%% If fib/1 is passed precisely the integer 0, then return 0
fib -> 0;
%% If fib/1 receives a negative number, then return the atom err_neg_val
%% Normally, such defensive coding is discouraged due to Erlang's 'Let
%% it Crash' philosophy; however, in this case we should explicitly
%% prevent a situation that will crash Erlang's runtime engine
fib when N < 0 -> err_neg_val;
%% If fib/1 is passed an integer less than 3, then return 1
%% The preceding two function signatures handle all cases where N < 1,
%% so this function signature handles cases where N = 1 or N = 2
fib when N < 3 -> 1;
%% For all other values, call the private function fib_int/3 to perform
%% the calculation
fib -> fib_int.
%% ---------------------------------------------------------------------
%% Private API
%% ---------------------------------------------------------------------
%% If fib_int/3 receives a 1 as its first argument, then we're done, so
%% return the value in argument B. Since we are not interested in the
%% value of the second argument, we denote this using _ to indicate a
%% "don't care" value
fib_int -> B;
%% For all other argument combinations, recursively call fib_int/3
%% where each call does the following:
%% - decrement counter N
%% - Take the previous fibonacci value in argument B and pass it as
%% argument A
%% - Calculate the value of the current fibonacci number and pass it
%% as argument B
fib_int -> fib_int.

Here's the same program without the explanatory comments:

-module.
-export.
fib -> 0;
fib when N < 0 -> err_neg_val;
fib when N < 3 -> 1;
fib -> fib_int.
fib_int -> B;
fib_int -> fib_int.

Quicksort

in Erlang, using list comprehension:

%% qsort:qsort
%% Sort a list of items
-module. % This is the file 'qsort.erl'
-export. % A function 'qsort' with 1 parameter is exported
qsort -> ; % If the list is empty, return an empty list
qsort ->
% Compose recursively a list with 'Front' for all elements that should be before 'Pivot'
% then 'Pivot' then 'Back' for all elements that should be after 'Pivot'
qsort ++
++
qsort.

The above example recursively invokes the function qsort until nothing remains to be sorted. The expression is a list comprehension, meaning "Construct a list of elements Front such that Front is a member of Rest, and Front is less than Pivot." ++ is the list concatenation operator.
A comparison function can be used for more complicated structures for the sake of readability.
The following code would sort lists according to length:

% This is file 'listsort.erl'
-module.
% Export 'by_length' with 1 parameter
-export.
by_length -> % Use 'qsort/2' and provides an anonymous function as a parameter
qsort -> length < length.
qsort-> ; % If list is empty, return an empty list
qsort ->
% Partition list with 'Smaller' elements in front of 'Pivot' and not-'Smaller' elements
% after 'Pivot' and sort the sublists.
qsort
++ ++
qsort.

A Pivot is taken from the first parameter given to qsort and the rest of Lists is named Rest. Note that the expression

is no different in form from

except for the use of a comparison function in the last part, saying "Construct a list of elements X such that X is a member of Rest, and Smaller is true", with Smaller being defined earlier as
fun -> length < length end
The anonymous function is named Smaller in the parameter list of the second definition of qsort so that it can be referenced by that name within that function. It is not named in the first definition of qsort, which deals with the base case of an empty list and thus has no need of this function, let alone a name for it.

Data types

Erlang has eight primitive data types:
;Integers: Integers are written as sequences of decimal digits, for example, 12, 12375 and -23427 are integers. Integer arithmetic is exact and only limited by available memory on the machine.
;Atoms: Atoms are used within a program to denote distinguished values. They are written as strings of consecutive alphanumeric characters, the first character being lowercase. Atoms can contain any character if they are enclosed within single quotes and an escape convention exists which allows any character to be used within an atom. Atoms are never garbage collected and should be used with caution, especially if using dynamic atom generation.
;Floats: Floating point numbers use the IEEE 754 64-bit representation.
;References: References are globally unique symbols whose only property is that they can be compared for equality. They are created by evaluating the Erlang primitive make_ref.
;Binaries: A binary is a sequence of bytes. Binaries provide a space-efficient way of storing binary data. Erlang primitives exist for composing and decomposing binaries and for efficient input/output of binaries.
;Pids: Pid is short for process identifiera Pid is created by the Erlang primitive spawn Pids are references to Erlang processes.
;Ports: Ports are used to communicate with the external world. Ports are created with the built-in function open_port. Messages can be sent to and received from ports, but these messages must obey the so-called "port protocol."
;Funs: Funs are function closures. Funs are created by expressions of the form: fun ->... end.
And three compound data types:
;Tuples: Tuples are containers for a fixed number of Erlang data types. The syntax denotes a tuple whose arguments are D1, D2,... Dn. The arguments can be primitive data types or compound data types. Any element of a tuple can be accessed in constant time.
;Lists: Lists are containers for a variable number of Erlang data types. The syntax denotes a list whose first element is Dh, and whose remaining elements are the list Dt. The syntax denotes an empty list. The syntax is short for .
;Records: Records provide a convenient way for associating a tag with each of the elements in a tuple. This allows one to refer to an element of a tuple by name and not by position. A pre-compiler takes the record definition and replaces it with the appropriate tuple reference.
Erlang has no method to define classes, although there are external libraries available.

"Let it Crash" coding style

In most other programming languages, software crashes have always been considered highly undesirable situations that must be avoided at all costs. Consequently, elaborate exception handling mechanisms exist to trap these situations and then mitigate their effects. This design philosophy exists because many of the foundational principles of software design were defined at a time when computers were single processor machines. Under these conditions, software crashes were indeed fatal. Given this basic constraint, it was perfectly natural therefore to develop programming styles in which a large proportion of the code was dedicated to detecting and then handling error situations. This in turn, led directly to the still widely popular coding style known as defensive programming.
However, the designers of Erlang realised that in spite of their undesirable effects, software crashes are much like death and taxes - quite unavoidable. Therefore, rather than treating a crash as a crisis situation that temporarily suspends all normal work until a solution is found, they reasoned it would make far greater sense to treat a crash in exactly the same manner as any other normal runtime event. Consequently, when an Erlang process crashes, this situation is reported as just another type of message arriving in a process' mail box.
This realisation led to Erlang's designers building a language with the following fundamental features:
The "Let it Crash" style of coding is therefore the practical consequence of working in a language that operates on these principles.

Supervisor trees

A typical Erlang application is written in the form of a supervisor tree. This architecture is based on a hierarchy of processes in which the top level process is known as a "supervisor". The supervisor then spawns multiple child processes that act either as workers or more, lower level supervisors. Such hierarchies can exist to arbitrary depths and have proven to provide a highly scalable and fault-tolerant environment within which application functionality can be implemented.
Within a supervisor tree, all supervisor processes are responsible for managing the lifecycle of their child processes, and this includes handling situations in which those child processes crash. Any process can become a supervisor by first spawning a child process, then calling erlang:monitor/2 on that process. If the monitored process then crashes, the supervisor will receive a message containing a tuple whose first member is the atom 'DOWN'. The supervisor is responsible firstly for listening for such messages and secondly, for taking the appropriate action to correct the error condition.
In addition, "Let it Crash" results in a style of coding that contains little defensive code, resulting in smaller applications.

Concurrency and distribution orientation

Erlang's main strength is support for concurrency. It has a small but powerful set of primitives to create processes and communicate among them. Erlang is conceptually similar to the language occam, though it recasts the ideas of communicating sequential processes in a functional framework and uses asynchronous message passing. Processes are the primary means to structure an Erlang application. They are neither operating system processes nor threads, but lightweight processes that are scheduled by BEAM. Like operating system processes, they share no state with each other. The estimated minimal overhead for each is 300 words. Thus, many processes can be created without degrading performance. In 2005, a benchmark with 20 million processes was successfully performed with 64-bit Erlang on a machine with 16 GB random-access memory. Erlang has supported symmetric multiprocessing since release R11B of May 2006.
While threads need external library support in most languages, Erlang provides language-level features to create and manage processes with the goal of simplifying concurrent programming. Though all concurrency is explicit in Erlang, processes communicate using message passing instead of shared variables, which removes the need for explicit locks.
Inter-process communication works via a shared-nothing asynchronous message passing system: every process has a "mailbox", a queue of messages that have been sent by other processes and not yet consumed. A process uses the receive primitive to retrieve messages that match desired patterns. A message-handling routine tests messages in turn against each pattern, until one of them matches. When the message is consumed and removed from the mailbox the process resumes execution. A message may comprise any Erlang structure, including primitives, tuples, lists, and functions.
The code example below shows the built-in support for distributed processes:

% Create a process and invoke the function web:start_server
ServerProcess = spawn,
% Create a remote process and invoke the function
% web:start_server on machine RemoteNode
RemoteProcess = spawn,
% Send a message to ServerProcess. The message consists of a tuple
% with the atom "pause" and the number "10".
ServerProcess !,
% Receive messages sent to this process
receive
a_message -> do_something;
-> handle;
-> io:format;
-> io:format
end.

As the example shows, processes may be created on remote nodes, and communication with them is transparent in the sense that communication with remote processes works exactly as communication with local processes.
Concurrency supports the primary method of error-handling in Erlang. When a process crashes, it neatly exits and sends a message to the controlling process which can then take action, such as for instance starting a new process that takes over the old process's task.

Implementation

The official reference implementation of Erlang uses BEAM. BEAM is included in the official distribution of Erlang, called Erlang/OTP. BEAM executes bytecode which is converted to threaded code at load time. It also includes a native code compiler on most platforms, developed by the High Performance Erlang Project at Uppsala University. Since October 2001 the HiPE system is fully integrated in Ericsson's Open Source Erlang/OTP system. It also supports interpreting, directly from source code via abstract syntax tree, via script as of R11B-5 release of Erlang.

Hot code loading and modules

Erlang supports language-level Dynamic Software Updating. To implement this, code is loaded and managed as "module" units; the module is a compilation unit. The system can keep two versions of a module in memory at the same time, and processes can concurrently run code from each. The versions are referred to as the "new" and the "old" version. A process will not move into the new version until it makes an external call to its module.
An example of the mechanism of hot code loading:

%% A process whose only job is to keep a counter.
%% First version
-module.
-export.
start -> loop.
loop ->
receive
->
loop;
->
Pid !,
loop;
code_switch ->
?MODULE:codeswitch
% Force the use of 'codeswitch/1' from the latest MODULE version
end.
codeswitch -> loop.

For the second version, we add the possibility to reset the count to zero.

%% Second version
-module.
-export.
start -> loop.
loop ->
receive
->
loop;
reset ->
loop;
->
Pid !,
loop;
code_switch ->
?MODULE:codeswitch
end.
codeswitch -> loop.

Only when receiving a message consisting of the atom code_switch will the loop execute an external call to codeswitch/1. If there is a new version of the counter module in memory, then its codeswitch/1 function will be called. The practice of having a specific entry-point into a new version allows the programmer to transform state to what is needed in the newer version. In the example, the state is kept as an integer.
In practice, systems are built up using design principles from the Open Telecom Platform, which leads to more code upgradable designs. Successful hot code loading is exacting. Code must be written with care to make use of Erlang's facilities.

Distribution

In 1998, Ericsson released Erlang as free and open-source software to ensure its independence from a single vendor and to increase awareness of the language. Erlang, together with libraries and the real-time distributed database Mnesia, forms the OTP collection of libraries. Ericsson and a few other companies support Erlang commercially.
Since the open source release, Erlang has been used by several firms worldwide, including Nortel and T-Mobile. Although Erlang was designed to fill a niche and has remained an obscure language for most of its existence, its popularity is growing due to demand for concurrent services.
Erlang has found some use in fielding massively multiplayer online role-playing game servers.