Welcome to UnixReview.com

Main Menu
  Home
  Archives
  Reviews
  Books
  Geek Links
  Contact Us

Sections
  Regular
  Expressions

  Tool of the Month
  Open Source
  Certification
  Shell Corner
  lost+found

Click Here!


Newsletter
Get the Newsletter
Get the Newsletter

  Print-Friendly Version

Regular Expressions: Erlang Is Worth a Look March 2002

by Cameron Laird and Kathryn Soraiz

Let's write highly-reliable, high-performance distributed applications. While we're at it, let's choose a development language with a "track record" of high productivity.

We'll use the Erlang programming language.

Erlang for Real

The name "Erlang" honors a Danish mathematician by that name, whose work in stochastic theories flourished at the beginning of the twentieth century. Although Erlang's name-recognition is low among U.S. programmers ("Erlang? Isn't that one of the new Winter Olypmics events?"), its accomplishments are plenty serious. First implemented in 1986 by an engineer at Ericsson Enterprise, Erlang is the language in which many production telecommunications and other mission-critical applications are written. Its use on general-purpose "personal computers" and servers is also growing.

One of the reasons for the interest in Erlang is that the software world is finally acknowledging the importance of distributed computation. This has been a good season for distributed computing -- Scientific American has popularized the topic; the new Usenet newsgroup comp.distributed has hosted a handful of worthwhile discussions since its launch in mid-February; and the Global Grid Forum (http://www.gridforum.org/) has become one of the hottest events in computing. Ideas about large-scale operating systems (OSes) are moving out of research laboratories; the .NET and Web Services marketing machines are starting to propagate technical content; and peer-to-peer and CORBA partisans are fighting back with significant engineering advances. This is territory that Erlang experts have been exploring for more than a decade.

Erlang was originally designed as a "language for programming distributed fault-tolerant soft real-time non-stop applications." That formulation correctly suggests there's more to Erlang than just its capacity for distributed calculations. Erlang is a functional programming language, with deep roots in logic and functional semantics that contribute to program correctness. Like such wildly different languages as Ada and Eiffel, Erlang's correctness characteristics make it easier for programmers to compose applications that behave predictably. Erlang generalizes correctness to "fault-tolerance" with its powerful "exception" mechanisms. Ericsson telephone switches need very high uptimes -- 99.999% and up. Erlang's fault-tolerance, related to the exceptions in Java and Python, helps achieve this level of reliability.

See for Yourself

What's it like to work in a language shaped for fault-tolerance, high productivity, and distribution? As with most of the subjects we choose for "Regular Expressions," the best answer is likely to be, "See for yourself." An open-source version of Erlang has been available since 1998, and extensive resources are available on the Web.

Source or binary downloads for the recent versions of the standard Erlang distribution are just over 10 megabytes in size. Although this is toward the top of the range for the languages "Regular Expressions" most often covers, it's significantly smaller than typical Java installations. In our experience, Unix generations of Erlang from source always go smoothly; whatever errors the latest releases have, they don't show up during installation or simple tests.

Once that's done, Erlang's "Hello, World" preliminaries go quickly enough. Erlang's home site even has a "Getting Started" page at http://www.erlang.org/starting.html. Like other typical modern interpreters, the Erlang language processor, erl, supports both interactive and "batch" operation. You can bring it up interactively as a convenient calculator:

     #> erl
     Erlang (BEAM) emulator version 5.1 [source]

     Eshell V5.1  (abort with ^G)   
     1> pwd().
     /usr/local/src/otp_src_R8B-0
     ok   
     2> ls().
     AUTHORS           EPLICENCE         Makefile          Makefile.in
     README            bin               bootstrap         config.cache
     config.log        config.status     configure         configure.in
     erts              lib               make
     ok
     3> 123 * 456 - 999.
     55089 
Notice that a period terminates every complete line.

Safe Power

The Erlang downloads are so large because the standard distribution is intended to "include batteries." Every Erlang installation builds in functions that mediate ASN encoding, manage CORBA transactions, perform cryptographic transformations, implement such standard networking protocols as FTP, HTTP, SNMP, and SSL, and even connect to Java classes.

This powerful library combines with Erlang's succinct expressivity to make remarkable results possible in just a few lines. It's hard to show Erlang at its best by simply looking at common programming tasks; it doesn't do the things that Perl or C do any better than those languages. Erlang comes into its own on applications that must run continuously (think of air-traffic control), cooperatively (across many machines), and without surprises. Erlang is largely immune to the buffer overflows, memory leaks, off-by-one, and other mundane errors that affect many applications.

A favorite example of Erlang's brevity is this generic client-server:

     -module(cs).
     -export([start/3, rpc/2, loop/2]).

     start(Name, Data, Fun) ->
         register(Name,
             spawn(fun() ->
                 loop(Data, Fun)
             end)).

     rpc(Name, Q) ->         
         Tag = ref(),
         Name ! -query, self(), Tag, Q),
         receive
             (Tag, Reply) -> Reply
         end.

     loop(Data, Fun) ->
         receive
             (query, Pid, Tag, Q) ->
                 (Reply, Data1) = Fun(Q, Data),
                 Pid ! (Tag, Reply),
                 loop(Data1, Fun)
     end. 
   

Invocation of start() launches a complete network endpoint. The code above handles communications and concurrency, while all that's left for an application developer is to bind Fun to a definition that executes the application-specific operation.

If your programs really, REALLY have to work right every time, consider using Erlang.

Administrative Wrap-Up

We're collecting notes and hyperlinks on Erlang at http://starbase.neosoft.com/~claird/comp.lang.functional/Erlang.html. One ambition for this page is to introduce a functional language for the "working programmer" in a way that makes the academic abstractions found in this specialty more appealing. As always, e-mail to us @regularexpressions.com is welcome. Incidentally, if you want to try out parallel programming in a more mainstream language than Erlang, consider PyPAR (http://datamining.anu.edu.au/pypar). This well-behaved Python extension was just released in its 1.0 version last month.

Next time, we'll look at a completely different approach to construction of correct programs: use of static syntax analyzers. Until then, good luck in solving your problems at a high level.

   
Home | Top

Click Here!
Copyright © 2002 UnixReview.com, UnixReview.com's Privacy Policy
Comments about the Web site: scoady@cmp.com
SDMG Web Sites: C/C++ Users Journal, Dr. Dobb's Journal, MSDN Magazine, Sys Admin, SD Expo, SD Magazine, UnixReview.com, Windows Developer, TPJ, BYTE.com

www3