Content deleted Content added
→"Let it crash" coding style: refnec not necessary => details are provided later in the article |
BrainStack (talk | contribs) Link suggestions feature: 3 links added. |
||
(42 intermediate revisions by 35 users not shown) | |||
Line 4:
| name = Erlang
| logo = Erlang logo.svg
| logo size =
| paradigms = [[Multi-paradigm programming language|Multi-paradigm]]: [[Concurrent programming|concurrent]], [[Functional programming|functional]]
| family =
Line 17:
| dialects =
| influenced_by = [[Lisp (programming language)|Lisp]], [[PLEX (programming language)|PLEX]],<ref>{{cite web |url=https://vimeo.com/97329186 |title=Joe Armstrong - Functional Programming the Long Road to Enlightenment: a Historical and Personal Narrative |first=N. D. C. |last=Conferences |date=4 June 2014 |publisher=Vimeo}}</ref> [[Prolog]], [[Smalltalk]]
| influenced = [[Akka (toolkit)|Akka]], [[Clojure]],<ref>{{cite web|url=http://radar.oreilly.com/2011/06/clojure-java-lisp-jvm.html|website=radar.oreilly.com|title=Clojure: Lisp meets Java, with a side of Erlang - O'Reilly Radar}}</ref> [[Dart (programming language)|Dart]], [[Elixir (programming language)|Elixir]], [[F Sharp (programming language)|F#]], [[Opa (programming language)|Opa]], [[Oz (programming language)|Oz]], [[Reia (programming language)|Reia]], [[Rust (programming language)|Rust]],<ref>{{Cite web |url=https://doc.rust-lang.org/reference/influences.html |title=Influences - The Rust Reference |website=The Rust Reference |access-date=2023-04-18}}</ref> [[Scala (programming language)|Scala]], [[Go (programming language)|Go]]
| file ext = .erl, .hrl
| license = [[Apache License 2.0]]
| website = {{
| wikibooks = Erlang Programming
}}
'''Erlang''' ({{IPAc-en|ˈ|ɜr|l|æ|ŋ}} {{respell|UR|lang}}) is a [[General-purpose programming language|general-purpose]], [[concurrent computing|concurrent]], [[Functional programming|functional]] [[High-level programming language|high-level]] [[programming language]], and a [[Garbage collection (computer science)|garbage-collected]] [[runtime system]]. The term Erlang is used interchangeably with Erlang/OTP, or [[Open Telecom Platform]] (OTP), which consists of the Erlang [[runtime system]], several ready-to-use components (OTP) mainly written in Erlang, and a set of [[Systems architecture|design principles]] for Erlang programs.<ref>{{cite web |url=http://erlang.org/doc/system_architecture_intro/sys_arch_intro.html#id58791 |title=Erlang – Introduction |website=erlang.org |access-date=6 February 2017 |archive-date=8 September 2019 |archive-url=https://web.archive.org/web/20190908105859/http://erlang.org/doc/system_architecture_intro/sys_arch_intro.html#id58791 |url-status=dead }}</ref>
The Erlang [[runtime system]] is designed for systems with these traits:
Line 34:
*[[Hot swapping#Software|Hot swapping]], where code can be changed without stopping a system.<ref>{{cite web |last1=Armstrong |first1=Joe |last2=Däcker |first2=Bjarne |last3=Lindgren |first3=Thomas |last4=Millroth |first4=Håkan |title=Open-source Erlang – White Paper |url=http://ftp.sunet.se/pub/lang/erlang/white_paper.html|archive-url=https://web.archive.org/web/20111025022940/http://ftp.sunet.se/pub/lang/erlang/white_paper.html |archive-date=25 October 2011 |access-date=31 July 2011}}</ref>
The Erlang [[programming language]] has
A normal Erlang application is built out of hundreds of small Erlang processes.
Line 41:
==History==
The name ''Erlang'', attributed to Bjarne Däcker, has been presumed by those working on the telephony switches (for whom the language was designed) to be a reference to Danish mathematician and engineer [[Agner Krarup Erlang]] and a [[Abbreviation#Syllabic abbreviation|syllabic abbreviation]] of "Ericsson Language".<ref name="hopl"/><ref>{{cite web |url=http://www.erlang.org/pipermail/erlang-questions/1999-February/000098.html |title=Erlang, the mathematician? |date=February 1999 }}</ref><ref>{{cite web |url=https://foldoc.org/Erlang |title=Free Online Dictionary of Computing: Erlang }}</ref> Erlang was designed with the aim of improving the development of telephony applications.<ref>{{cite web|url=http://erlang.org/course/history.html|title=History of Erlang|website=Erlang.org}}</ref> The initial version of Erlang was implemented in [[Prolog]] and was influenced by the programming language [[PLEX (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 (Erlang virtual machine)|BEAM]] [[virtual machine]] (VM), which compiles Erlang to C using a mix of natively compiled code and [[threaded code]] to strike a balance between performance and disk space.<ref>{{cite
[[File:Robert Virding and Joe Armstrong, 2013.jpg|thumb|Robert Virding and Joe Armstrong, 2013]]
In February 1998, Ericsson Radio Systems banned the in-house use of Erlang for new products, citing a preference for non-proprietary languages.<ref>{{cite thesis|url=https://cogsys.uni-bamberg.de/team/schmid/uoshp/lehreuos/fp01-www/fp-referate/erlang/bjarnelic.pdf#page=45|title=Concurrent Functional Programming for Telecommunications: A Case Study of Technology Introduction|first=Bjarne|last=Däcker|date=October 2000|publisher=Royal Institute of Technology|page=37}}</ref> The ban caused Armstrong and others to make plans to leave Ericsson.<ref name="questions"/> In March 1998 Ericsson announced the AXD301 switch,<ref name="hopl"/> containing over a million lines of Erlang and reported to achieve a [[high availability]] of [[nines (engineering)|nine "9"s]].<ref>
{{cite web |url=http://www.rabbitmq.com/resources/armstrong.pdf |title=Concurrency Oriented Programming in Erlang |date=9 November 2002}}
</ref> In December 1998, the implementation of Erlang was open-sourced and most of the Erlang team resigned to form a new company, Bluetail AB.<ref name="hopl"/> Ericsson eventually relaxed the ban and re-hired Armstrong in 2004.<ref name="questions">{{cite web |url=http://erlang.org/pipermail/erlang-questions/2006-July/021368.html |title=question about Erlang's future |date=6 July 2010}}</ref>
In 2006, native [[symmetric multiprocessing]] support was added to the runtime system and VM.<ref name="hopl"/>
===Processes===
Erlang applications are built of very lightweight Erlang processes in the Erlang runtime system
Joe Armstrong, co-inventor of Erlang, summarized the principles of processes in his [[Doctor of Philosophy|PhD]] [[thesis]]:<ref>
*Everything is a process.
Line 67 ⟶ 65:
*Processes do what they are supposed to do or fail.
Joe Armstrong remarked in an interview with Rackspace in 2013: "If [[Java (programming language)|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 (telecommunication)|LTE]] mobile networks worldwide and also by [[Nortel]] and [[
Erlang is used in [[RabbitMQ]]. As [[Tim Bray]], director of Web Technologies at [[Sun Microsystems]], expressed in his keynote at [[O'Reilly Open Source Convention]] (OSCON) in July 2008:
Line 77 ⟶ 75:
Erlang is the programming language used to code [[WhatsApp]].<ref>{{cite web |url=https://www.fastcompany.com/3026758/inside-erlang-the-rare-programming-language-behind-whatsapps-success |title=Inside Erlang, The Rare Programming Language Behind WhatsApp's Success |work=fastcompany.com |date=21 February 2014 |access-date=12 November 2019}}</ref>
It is also the language of choice for [[Ejabberd]] – an [[XMPP]] messaging server.
The Elixir programming language compiles into BEAM byte code (via Erlang Abstract Format).<ref>{{Cite web |title=Erlang/Elixir Syntax: A Crash Course |url=https://elixir-lang.org/crash-course.html |access-date=2022-10-10 |website=elixir-lang.github.com |language=en}}</ref>▼
▲
Since being released as open source, Erlang has been spreading beyond telecoms, establishing itself in other vertical markets such as FinTech, gaming, healthcare, automotive,
==Functional programming examples==
Line 119:
%% The order in which these function signatures are declared is a vital
%% part of this module's functionality
fib(0) -> 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
fib(N) when N < 0 -> err_neg_val;
%% If fib/1 is passed
▲fib(0) -> 0;
%% For all other values, call the private function fib_int/3 to perform
%% the calculation
fib(N) -> fib_int(N-1, 0, 1).
%% ---------------------------------------------------------------------
Line 142 ⟶ 136:
%% ---------------------------------------------------------------------
%% If fib_int/3 receives
%% return the value in argument B. The
%% disregard its value.
▲fib_int(1, _, B) -> B;
%% For all other argument combinations, recursively call fib_int/3
%% where each call does the following:
%% - decrement counter N
%% -
%% - pass the sum of the second and third arguments as the new
%%
fib_int(N, A, B) -> fib_int(N-1, B, A+B).
</syntaxhighlight>
Omitting the comments gives a much shorter program.
<syntaxhighlight lang="erlang">
Line 164 ⟶ 156:
-export([fib/1]).
fib(N) when N < 0 -> err_neg_val;
fib(
fib(N) -> fib_int(N-1, 0, 1).
fib_int(
fib_int(N, A, B) -> fib_int(N-1, B, A+B).
</syntaxhighlight>
Line 250 ⟶ 241:
;Strings: Strings are written as doubly quoted lists of characters. This is syntactic sugar for a list of the integer [[Unicode]] code points for the characters in the string. Thus, for example, the string "cat" is shorthand for <code>[99,97,116]</code>.<ref>{{cite web |url=http://erlang.org/doc/apps/stdlib/unicode_usage.html#string-and-character-literals |title=String and Character Literals |access-date=2 May 2015}}</ref>
;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.
=="Let it crash" coding style==
Line 260 ⟶ 249:
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 <code>erlang:monitor/2</code> on that process. If the monitored process then crashes, the supervisor will receive a message containing a tuple whose first member is the atom <code>'DOWN'</code>. The supervisor is responsible firstly for listening for such messages and
==Concurrency and distribution orientation==
Line 274 ⟶ 263:
|quote=Erlang is conceptually similar to the occam programming language, though it recasts the ideas of CSP in a functional framework and uses asynchronous message passing.
|doi-access=free
}}</ref> Processes are the primary means to structure an Erlang application. They are neither [[operating system]] [[Process (computing)|processes]] nor [[Thread (computing)|threads]], but [[Light-weight process|lightweight processes]] that are scheduled by BEAM. Like operating system processes (but unlike operating system threads), they share no state with each other. The estimated minimal overhead for each is 300 [[Word (computer architecture)|words]].<ref>{{cite web |title=Erlang Efficiency Guide – Processes |url=http://www.erlang.org/doc/efficiency_guide/processes.html|archive-url=https://web.archive.org/web/20150227174813/http://www.erlang.org/doc/efficiency_guide/processes.html|archive-date=27 February 2015}}</ref> 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]] (RAM; total 800 bytes/process).<ref>{{cite web |first=Ulf |last=Wiger |title=Stress-testing erlang |url=https://groups.google.com/group/comp.lang.functional/msg/33b7a62afb727a4f?dmode=source |work=comp.lang.functional.misc |access-date=25 August 2006 |date=14 November 2005}}</ref> Erlang has supported [[symmetric multiprocessing]] since release R11B of May 2006.
While [[Thread (computing)|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 [[Lock (computer science)|locks]] (a locking scheme is still used internally by the VM).<ref>{{cite web |title=Lock-free message queue |url=http://erlang.2086793.n4.nabble.com/Lock-free-message-queue-td2550221.html |access-date=23 December 2013 |archive-date=24 December 2013 |archive-url=https://web.archive.org/web/20131224104549/http://erlang.2086793.n4.nabble.com/Lock-free-message-queue-td2550221.html |url-status=dead }}</ref>
[[Inter-process communication]] works via a [[shared-nothing architecture|shared-nothing]] [[asynchronous method dispatch|asynchronous]] [[message passing]] system: every process has a "mailbox", a [[queue (data structure)|queue]] of messages that have been sent by other processes and not yet consumed. A process uses the <code>receive</code> 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 (integers, floats, characters, atoms), tuples, lists, and functions.
Line 308 ⟶ 297:
==Implementation==
The official [[reference implementation]] of Erlang uses [[BEAM (Erlang virtual machine)|BEAM]].<ref>{{cite web |url=http://erlang.org/doc/reference_manual/code_loading.html#id90080 |title=Erlang – Compilation and Code Loading |website=erlang.org |access-date=2017-12-21}}</ref> 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 (HiPE) at [[Uppsala University]]. Since October 2001 the HiPE system is fully integrated in Ericsson's Open Source Erlang/OTP system.<ref>{{cite web |title=High Performance Erlang |url=http://www.it.uu.se/research/group/hipe/ |access-date=26 March 2011}}</ref> 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==
Line 370 ⟶ 359:
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 Networks|Nortel]] and [[
Erlang has found some use in fielding [[massively multiplayer online role-playing game]] (MMORPG) servers.<ref>{{cite web
|last=Clarke
Line 388 ⟶ 377:
* [[Mix (build tool)]]
* [[Phoenix (web framework)]]
* [[Gleam (programming language)]] – a general-purpose, concurrent, functional high-level programming language that compiles to Erlang
==References==
Line 394 ⟶ 384:
==Further reading==
{{Refbegin}}
* {{Cite thesis |first=Joe |last=Armstrong |title=Making reliable distributed systems in the presence of software errors |url=https://www.sics.se/~joe/thesis/armstrong_thesis_2003.pdf |type=PhD |publisher=The Royal Institute of Technology, Stockholm, Sweden |year=2003 |access-date=13 February 2016 |url-status=dead |archive-url=https://web.archive.org/web/20150323001245/https://www.sics.se/~joe/thesis/armstrong_thesis_2003.pdf |archive-date=23 March 2015 }}
* {{cite book |doi=10.1145/1238844.1238850 |isbn=978-1-59593-766-7 |title=Proceedings of the third ACM SIGPLAN conference on History of programming languages – HOPL III |year=2007 |chapter=A history of Erlang |last=Armstrong |first=Joe |pages=6–1|s2cid=555765 }}
* [http://www.erlang.se/publications/bjarnelic.pdf Early history of Erlang] {{Webarchive|url=https://web.archive.org/web/20190829000127/http://www.erlang.se/publications/bjarnelic.pdf |date=29 August 2019 }} by Bjarne Däcker
*{{cite journal |last1=Mattsson |first1=H. |last2=Nilsson |first2=H. |last3=Wikstrom |first3=C. |year=1999 |title=Mnesia – A distributed robust DBMS for telecommunications applications |journal=First International Workshop on Practical Aspects of Declarative Languages (PADL '99) |pages=152–163}}
* {{cite book
Line 416 ⟶ 406:
|archive-url=https://web.archive.org/web/20120306002430/http://www.erlang.org/erlang_book_toc.html
|archive-date=6 March 2012
}}
* {{cite book
|first=Joe
Line 442 ⟶ 432:
|isbn=978-0-596-51818-9
|url=http://www.erlangprogramming.org
}}
* {{cite book
|first1=Martin
Line 467 ⟶ 457:
|url=http://www.ibm.com/developerworks/opensource/library/os-erlang1/index.html
|access-date=10 May 2011
}}
* {{cite web
|last=Martin
Line 476 ⟶ 466:
|date=17 May 2011
|url=http://www.ibm.com/developerworks/opensource/library/os-erlang2/index.html
|access-date=17 May 2011
}} * {{cite web
|last=Wiger
Line 486 ⟶ 477:
|url=http://www.erlang.se/publications/Ulf_Wiger.pdf
|access-date=16 September 2014
|archive-date=19 August 2019
}}▼
|archive-url=https://web.archive.org/web/20190819130310/http://www.erlang.se/publications/Ulf_Wiger.pdf
|url-status=dead
▲ }}
{{Refend}}
Line 492 ⟶ 486:
{{Commons category}}
{{Wikibooks|Erlang Programming}}
* [[iarchive:ErlangTheMovie|"Erlang: The Movie"]]
* {{Official website}}
|