home mail me! RSS (2.0) feed

Erlang - the best or worst of two worlds?

A friend of mine has become an Erlang aficionado lately, and has - deliberately or not - pulled me into Erlang development ;-) I had not touched Erlang in a long time but always knew there was something about it that bothered me some twelve years ago. I forgot what, but now remember.

This post is an attempt to clarify what bothered me - and still does to some extent - about this language. Before doing that, it is important to note that whataver deificiencies Erlang might have, it is - in my extremely humble opinion - currently one of three viable functional languages for big scale development, the other two being F# and Scala, relying on imperative execution environments (Java VM and .NET CLR..) Yes, there are Lisp implementations out there, but they are losing ground. Not like the good old Allegro Common Lisp days :-(

As Philip Wadler and Simon Marlow put it, in a proposal to extend Erlang’s type system:

We can stop waiting for functional languages to be used in practice - that day is here!

Note: I am here focusing on the language, not the runtime execution environment, which is far ahead of any other functional programming language, or most languages for that matter. What other language provides light-weight processes with distributed message passing out of the box, while supporting hot swap of code? Did I forget to mention a transparently distributed database system (Mnesia)?

The Erlang approach is to bring academics into a pragmatic setting. Those are the two conceived worlds:

  1. the academic world of functional programming and pattern matching
  2. the pragmatic - dare I say “agile” world - of dynamically typed languages

The promise of Erlang is to be the best of both worlds. The Erlang developer should then be the prime example of The Pragmatic Academic.

I do not agree. I argue that Erlang is extremely clumpsy for problems involving a host of distinct notions, i.e., needing many different types, often intricately dependent on each other, ontologically or not.

The only compound types available to the Erlang developer are the tuples and lists. Just like with tables in Lua, you can emulate your own complex types via these Erlang tuples. That is the antipod to abstract data types, via the extremely extrovert representation, and is very hard to maintain for more complex types, especially since there is no type checking. With that “record”-based approach (no, I am not talking about the formal record that also exists Erlang…), it comes as no surprise that it is very susceptible to RDBMS, as shown by the eminent ErlyDB. Most developers take the approach of using the first element of the tuple as the “type”, or functor as it would be called in the Prolog days.

This lack of user-defined compound types has been observed by some Big Boys in the FP world, such as Marlow and Wadler.

But cannot dynamic typing be quite empowering? Yes, in one case and one case only: heterogeneous lists. In all other cases, the variable will have a “type” intended by the operations performed on it, and if those operations are hard to gather into “classes” or “types”, then one can use generic types in modern typed languages. I.e., just because you do not have to state a type does not mean that you do not have to have a specific “type” in mind when designing and coding. You do. If that mental “type” of yours is a set of possible operations, it is called a Concept in Generic Programming, and often implemented via templates or generics.

Ok, you save a few key strokes by not adorning the variable with a type, but so can type deduction do for you.

What about new types introduced in runtime? This is a better defense of dynamic types. Erlang allows for introduction of, or alteration of, code in runtime. Just like C# (via .NET), Java and the good old Dylan system. In fact, most dynamic languages do, like JavaScript and Lisp. Erlang developers seem to forget that this a quite non-unique feature. Nevertheless, introduction of new types with static typing requires explicit introspection, like using the Method class of Java. I.e., one cannot “type” for the unexpected. But, and this is an important note, dynamic typing does not save you here. You still cannot code for the unexpected. I.e., introducing types not compatible with the implicit types implied by the operations involved will still wreck the code!

This latter problem, of not being able to code for the unexpected when new code is introduced in runtime, is equally valid for alteration of code outside runtime. I.e., what happens if you suddenly decide to change the representation of a notion, such as a adding a second “e-mail” field to “customer”? You would have to go to every single point where you used those tuples, yes, all those case loci, and change the code. It is no coincidence that quite smart people introduced abstract data types ;-) In Erlang everything is algebraic or “exposed to the outside world”. For further discussion about algebraic data types, look at this earlier post. To put it in a database-centric way: Erlang deal with non-normalized records only.Admittedly, you can embed types via accessors but that is not the “Erlang way” of doing things.

Besides these issues of dynamic types and restrictive types, I was bothered by a certain non-FP-ness of Erlang. When I tried to recall what that bother of mine was, I wrongly concluded that it must be that no anonymous function can be created. This is - obviously - not the case, one can easily do that via fun (x) -> x*2. Looking into Erlang again, I realize that it was the lack of curried functions that annoyed me. :-)

Leaving the language for a brief moment, the execution environment is quite remarkable. So, if we can add the following two notions to Erlang, it would quickly become my new best friend:

  1. typing - as proposed by Wadler and Marlow
  2. curried functions

I will stop whining now and instead help out in making the Erlang world a better place. The beams call me!

Interesting Take on Erlang by David Bergman said,

September 5, 2006 @ 12:33 pm

[…] My friend and former collegue, David Bergman, has an interesting take on Erlang in his blog: Erlang: the Best or Worst of Two Worlds? […]

Another Interesting Erlang Day on Reddit said,

September 5, 2006 @ 3:37 pm

[…] Today, I saw 4 Erlang-related articles on programmin.reddit.com: earlier this morning, my article OO, FP, Erlang, and Me was somewhere in the bottom half of the links. It may have been higher at some point — I don’t know. At number 2 (and it used to be at number 1) is Joe Armstrong’s Why I Don’t Like Shared Memory. At number 9) is Ask Reddit: is Erlang hype the next Ruby hype? I seriously don’t think that Erlang’s “hype” is anywhere close to Ruby’s hype. Erlang’s “hype” is basically my blog, wagerlabs.com and now Joe Armstrong’s blog. I don’t think we are trying to “hype” Erlang as much as raise awareness to the unique capabilities of this relatively under-hyped language, but other people may see it differently. At number 19) is David Bergman’s Erlang: The Best or Worst of Two Worlds?. It may have something to do with my mentioning this article on my blog, but maybe it was discovered by the poster independently. […]

igouy said,

September 7, 2006 @ 4:37 pm

My impression is that what bothers you is simply that Erlang is not statically type checked.

The comments about abstract data types seem mostly to be a concern that although we could provide a module with functions that create and manipulate some particular data type, the data type isn’t opaque - we cannot force the programmer to use those functions, the programmer might figure out some other way to access the data structure. iow Erlang does not enforce abstract data types but does allow abstract data types.

afaict Wadler and Marlow’s proposal never gained acceptance among Erlang programmers. In contrast, static analysis (including type analysis) did gain acceptance, see “The DIALYZER: a DIscrepancy AnaLYZer for ERlang programs”
http://www.it.uu.se/research/group/hipe/dialyzer/

davber said,

September 7, 2006 @ 5:11 pm

Well, there were two objections embedded in my whining: (1) the “simple” fact that Erlang is not statically typed and (2) the lack of user-defined structures. Because it is feasible to have the latter while being dynamically typed. See Lisp for an example.

Regardig the abstract data types, yes, one can - obviously - aways have developers use functions to access data, but (1) there are no such built-in constructs in the language (compare with the “signature” of ML) and (2) that is not the modus operandi among Erlang developers. Ok, I shoud not blame the language for what developers do or not do ;-)

igouy said,

September 8, 2006 @ 5:45 pm

Let’s skip #1 “Erlang is not statically typed”

I’m a bit puzzled about #2
If we take the simple Dictionary example from the Erlang book (program 4.1 on page 57 http://www.erlang.org/download/erlang-book-part1.pdf ) then the module dictionary seems to provide a data type - what it fails to do is enforce consistent use of that data type, we could use the functions on a data structure that wasn’t created use the dictionary new/0 function.

Long ago I used Modula-2 which has a much stricter notion ADT :-)

(I’m not in a position to claim knowledge of “the modus operandi among Erlang developers”.)

RSS feed for comments on this post · TrackBack URI

Leave a Comment

You must be logged in to post a comment.