Saturday, September 6, 2008

Erlang

Just bought Joe Armstrong's book on Erlang and have started working through it. Erlang's an interesting language: although it's been around for years it's recently seen a sudden rise in popularity. Why the sudden interest? Concurrent programming. Moore's law is disintegrating for single core processors, and multi core/multi-processor machines are seen as the way forward. For those used to programming in traditional imperative languages (C, C#, Java, ...) multi-threading has always been difficult and error prone. Erlang was conceived from the beginning to support hundreds, thousands, even millions of concurrent processes, communicating with each other by passing messages. Armstrong contends this makes concurrency much, much simpler than trying to force it on top of the inherently sequential, shared memory model underpinning imperative languages.

Erlang is a functional language which, among other things, means it doesn't support mutable state. 'Variables' aren't; once assigned a value, it's not possible to re-assign a variable to a new value. So variables would be more accurately described as 'immutable single assignment references', although that's admittedly a bit less catchy.

As someone schooled in mutable state since Amstrad CPC464 basic, that's a violent, disruptive change to the mental model. In fact, it's a disruptive change for anyone used to observing the world around them as entities with properties that change over time: bank account balances that vary from day to day, people whose age increases every birthday.

It's a practical problem for functional languages too, since for most any program to be useful it needs to persist or change the state of the world around it. (That's why Haskell has its monad system).

Erlang supports programs that need to store, update and use mutable state; it has its own relational database system (Mnesia) which - since it's written in Erlang - is fully ditributable, concurrent and fault tolerant. Oh, and it doesn't need an object-relational mapping layer either since it's built to work natively with Erlang's type system.

I'm looking forward to getting into Erlang. The concurrent, message passing model is very appealing conceptually and offers lots of possibilities in the new multi-core world. But more than anything else in the book, I'm looking forward to getting my head around marrying the functional model with mutable state.

No comments: