Saturday, 11 August 2007

Erlang's change of fortunes

Today morning I had a conversation with my distinguished colleague Stefan Z. while hanging out at the coffee-vending machine in the tea-kitchen at our client's office. As we are both working for the telecom industry, we came to discuss Erlang. No, not that celebrated guy, but a rather obscure programming language designed at the Ericsson Corporation.

For the fun of it, let's first look at some snippets of Erlang code!

define a data structure:
  Student={person,{name,{first,yakkety},{last,yak}},{footsize,1}}.
access a member of data struct (by pattern matching, cool!!!):
  {_,{_,{_,HisName},_},_} = Student.
printing yakkety:
  io:fwrite("his name is: ~w~n", [HisName]).
define a list:
  Fruits = [{apples,10},{pears,6},{prunes,3}].
append something to a list:
  Fruits1 = [{oranges,4},{lemons,1}  Fruits].
get the head of the list (by pattern matching, cool!!!):
  [ListHeadFruits2] = Fruits1.
simple function:
  incr(Num) -> Num + 1.
recursive function:
  fac(0) -> 1;
fac(Num) when Nun > 0 -> N * fac(Num-1).
infinite loop receiving messages (by pattern matching, cool!!!) and sending responses back (by the !operator):
  loop() ->
receive
{From, {request, Param1, Param2}} ->
From ! {self(), Param1 + Param2},
loop();
end.
a foreach loop (note the lambda function definition between fun and end!):
  extract2ndElements(List) ->
lists:map(fun({_,Second}) -> Second end, List).

Ok, it was fun, but we mustn't digress too much...

Erlang in itself is rather a paradox: you normally associate Telco equipment programming with the lowest of the lowest: asm, C99 or EC++ (castrated C++). Now, there come some guys in the 80-ties and to the corporate management they can sell a functional language for this task! This alone is incredible! But wait, there's more! Up to now Erlang was a niche language used quite exclusively at Ericsson. Now this seems to be changing. But first we need some history.

In the 80-ties Erlag was designed to be slow but reliable. This is due to the very nature of the functional languages: you don't have shared state, no side effects, all data is copied between functions. For safety's sake even the variables can be assigned only once! If you apply that to parallel computing, you don't have to worry about synchronization and critical sections - the "share nothing semantics". But it will be rather slow. In this case it was a deliberate decision: they wanted not simply a reliable language, but a "highly reliable language"! And a distributed one. So to speak a natural fit for the Telco environment. As an example: the AXD301 switch achieved the incredible 99,9999999% realiability! It's 9 nines availability! And we were struggling to achieve 5 nines* availability with C++ on CarrierGrade, High Avalability (HA) Linux and a custom HA-middelware platform in my last project! By the way, the AXD301 software has 1.7 million lines of Erlang, making it the largest functional program ever written**! Really impressive stuff IMHO.

I guess you have read (or heard about) Herb Sutter's article titled "The free lunch is over"***. So you know that the "next big thing" in software is supposed to be multicore scalability. So hear up now: as the multicores are comming pretty cheap now, Erlang can be at last both fast and reliable as it can be easily distributed on several cores! It's definitely good news! We've seen the incredible reliability which can be achieved, and have a natural programming model for distributed computation. Why? In short, as there is no shared data:

  1. programs are easily distributable,

  2. easily made fault tolerant by composing them from worker and observer processes,

  3. easily made scalable (because of 1. we simply add more processors).**

So could distributed computing be the saviour of the functional programming? Think about Google's MapReduce!

---
* For those not working with HA systems:
      Availability%   Downtime per year
99.9999 30 seconds !!!!
99.999 5 minutes
99.99 50 minutes
99.9 9 hours
** says Phillip Wadler: http://wadler.blogspot.com/2005/05/concurrency-oriented-programming-in.html

*** on DDJ or www.gotw.ca/publications/concurrency-ddj.htm

Monday, 6 August 2007

iPhone presentation with afterthoughts

Yesterday me and my distinguished colleague Stefan Z. both saw the iPhone for the first time (over here in Germany it isn't available easily yet, it'll come sometime in November). It was presented to us by our co-consultant in the telco field, a long-time Mac and Objective-C freak. He is pushing Apple technology wherever he can, and has got considerable succes with his Mac-based telco testbed infrastrustructure recently. He's the archetypical nerd - totally enthusiastic about technonolgy. To help you to go into the mood of the conversation I offer a couple of juicy citations: "...y'all haven't got a clue!", "...here ends your tether but I'm only beginning.", "...all the telco companies can shut down now!". He showed us the iPhone GUI and it was impressive: it was just as it always should be! You can operate it holding it in one hand and just using your thumb on the touch screen!

His basic message was this: no other company can duplicate this on a mobile phone (or on any other operating system for that matter)!

Ok, sometimes he tends to massive exaggeration but this one started me thinking. I guess my coleague is essentially in the right, and it boils down to the technology (not management). As I don't know much about iPhone I'll be shamelessly wallowing in conjecture now, but read on. The first thing I thought about was: yeah, that wouldn't be certainly possible with JavaPhone! They are doing it in Objective C and Cocoa as "Java and GUI don't mix" and "Friends don't let friends Swing" (guess who said this*). And Java is the programming language of choice today! You can even do real-time programing with JRockit's deterministic garbage collection of late!

Java proponents do not conceal that Java's stronghold is more and more the enterprise application computing**. On the other side Java critics may say that this stronghold is rather a kind of ghetto (Java is the next COBOL - where did I hear that?). Steve Jobs said: "Java's not worth building in. Nobody uses Java anymore. It's this big heavyweight ball and chain..."*** Ok, an exaggeration, we are using Java on a project here, but you get the idea: Java is now a language for big, heavyweight, corporate applications. Nothing exciting to be expected here. Is there another language/system which can do something that cool? Yes, on the desktop (well browser...) you can do some cool things with Flash, maybe with JavaScript. But for the regular GUI there's nothing comparable I fear. So the Objective-C freak can be in the right.

---
* it was the creator of Tomcat and Ant
** for example "Why Java?" on http://www.wantii.com/wordpress/?p=5
*** see http://www.informit.com/discussion/index.asp?postid=d1e63fde-10d5-404b-8a14-6ff0b92c1ee1 for some lively discussion on that phrase, you can google for "Java? It's so 90-ties" for some more Java critique


Reprise:


As I said, I didn't new first thing about iPhone at the time I wrote this entry. But gradually I've learnt some new things, and now I can compare my then guesses to the facts.

1. Yes, I was basically right: iPhone uses Cocoa*, and you'll program Cocoa with Objective-C. So you cannot program it with Java - the iPhone is just 100% anti-Java. But curiously, you can do it anyway, only through an detour. Just take the GWT (Google Web Toolkit), write an Web application in Java and GWT will translate it to JavaScript, which will be executed by iPhone's Safari browser!** I find it somehow a strange twist of fate! BTW, the Google Phone*** is rumored to be 100% pro-Java...

2. Yes, I was partially right about GUI programming: yes, Java isn't up to the rask, but Sun seems to have noticed this and works on Java-FX: "a new family of Sun products based on Java technology and targeted at the high impact, rich content market"****. The reasons why a new take was needed were summed up as a following series of questions:****
  • Why does it take a long time to write GUI programs?
  • How can we avoid the “Ugly Java technology GUI” stereotype?
  • Why do Flash programs look different than Java platform programs?
  • Why does it seem easier to write web apps than Swing programs?
  • How can I avoid having an enormous mass of listener patterns?
So these are some problems! As it seems, Java isn't anymore the one-fits-all language of yore, but is complemented by a host of new languages using the Java-VM (FX, Groovy, JRuby...). So maybe it's really: "Java is the new COBOL"?

---
* see: http://cocoasamurai.blogspot.com/2007/01/how-i-think-iphone-software-works.html
** http://groups.google.com/group/Google-Web-Toolkit/web/gwt-iphone-pretty-tasty
*** for some cool pics of Google Phone see: http://www.google-phone.com/ or - even better - http://www.seekxl.de/blog/2007/08/05/google-phone-fotos-vom-google-phone/
**** Sun's Java-FX presentation: http://jazoon.com/jazoon07/en/conference/presentationdetails.html?type=sid&detail=2161