A personal note

May 15, 2007 by jao

If this blog had a written editorial policy, one of its first rules would be not to bore readers with details from my personal life. Except to tell them that i’m moving to Zurich to work at Google on next July. But fear not: i’m not writing yet another excruciatingly detailed report about my selection process. It was fun, exhausting and worth the effort. You don’t need to be a genius; but a good, solid background will help. That’s essentially all.

As it happens, it all began when someone at Google saw this blog and contacted me. In that way, programming musings has eventually made a dent in my tiny personal universe. Which leads me to the real reason of this post: to say thank you to all the readers, the people writing comments, those posting or voting my entries in reddit, the bloggers linking to this site and overstating its merits. In sum, thanks to all those of you who, in one way or another, have encouraged me and kept this blog rolling.

All i can say is that i’ll try hard not to disappear on July ;-).

Erlang, Termite and a Blog

May 14, 2007 by jao

In his soon-to-be-published book, Joe Armstrong proposes the following exercise:

Write a ring benchmark. Create N processes in a ring. Send a message round the ring M times. So that a total of N * M messages get sent. Time how long this takes for different values of N and M.

and he adds

Write a similar program in some other programming language you are familiar with. Compare the results. Write a Blog and publish the results on the Internet!

Since i happen to be learning Erlang and, besides, know of another programming language providing Erlang-like primitives, i thought i’d take Joe’s bait and give you a little taste of how Erlang feels to a newcomer.

This is my implementation of the ring in Erlang. If you’re an Erlang hacker, be prepared to be scared: this is my first longer-than-ten-lines program–but, please, do not hesitate to criticize it as deserved!.

-module(ring).
-export([make_ring/2]).

make_ring(N, M) -> spawn(fun() -> sender(N, M) end).

sender(N, M) ->
    FirstPid = self(),
    NextPid = spawn(fun() -> tunnel(N, 2, FirstPid) end),
    statistics(runtime),
    statistics(wall_clock),
    do_times(M, 0, fun(I) -> NextPid ! I end),
    NextPid ! done,
    receive done -> done end,
    {_, Time1} = statistics(runtime),
    {_, Time2} = statistics(wall_clock),
    U1 = Time1 * 1000,
    U2 = Time2 * 1000,
    io:format(”Total time=~p (~p) microseconds~n”, [U1, U2]).

do_times(N, N, _) -> done;
do_times(N, J, Fun) -> Fun(J), do_times(N, J+1, Fun).

tunnel(N, N, FirstPid) -> tunnel(FirstPid);
tunnel(N, J, FirstPid) ->
     tunnel(spawn(fun() -> tunnel(N, J+1, FirstPid) end)).

tunnel(Pid) ->
    receive
        done -> Pid ! done;
        Any -> Pid ! Any, tunnel(Pid)
    end.

Here you can see some of Erlang’s niceties: first class, anonymous functions (using the fun() -> [body] end lambda constructor); function definition by cases (a la Haskell: guards are also admitted) and pattern matching; message mailboxes using receive (also using pattern matching) and the send operator !; easy process creation with spawn; and the pretty no-frills but extremely convenient module system (one would write ring:make_ring(A,B) to call our ring launcher: the Erlang shell will find and load the ring module as long as the file is in the load path). You may also have noticed that there is no variable mutations, or that lower case atoms (like done) are automatically interpreted as symbols. Tuples also make an appearance: they are constructed using braces (as in {_, Time1}) and you assign values to its components, again, by pattern matching. And, oh, those tail recursive calls to tunnel and do_times are properly optimized by the Erlang interpreter.

Although the above is just a subset of the language’s features, it’s enough to show why i like Erlang: it reminds me of Scheme in many ways, while borrowing some interesting bits from ML languages and even Prolog. On top of it, it fulfills Alan Perlis desideratum on programming languages: it changes the way you think, as recently noticed by one of my favourite bloggers. (It is also worth mentioning that Erlang comes charged with a pretty nice library, excellently documented, that has recently gained a slick search interface.)

But back to Joe’s problem. As it happens, there’s ‘one other language i’m familiar with’ (Scheme) that provides and Erlang-like library (Gambit’s Termite), and rewriting the program was a breeze:

(define (make-ring n m) (spawn (lambda () (sender n m))))

(define (sender n m)
  (define (display-time msg start end)
    (display (list msg (- end start)))
    (newline))
  (let* ((fist-pid (self))
         (next-pid (spawn (lambda () (tunnel n 2 fist-pid))))
         (rt (real-time))
         (ct (cpu-time)))
    (let loop ((m m))
      (cond ((= 0 m)
             (! next-pid 'done)
             (recv ('done
                    (let ((nct (cpu-time))
                          (nrt (real-time)))
                      (display-time "CPU time: " ct nct)
                      (display-time "Real time: " rt nrt)))))
            (else (! next-pid m)
                  (loop (- m 1)))))))

(define (tunnel n j first-pid)
  (cond ((= n j) (send-recv first-pid))
        (else (send-recv (spawn (lambda ()
                                   (tunnel n (+ 1 j) first-pid)))))))

(define (send-recv pid)
  (recv
   ('done (! pid 'done))
   (x (! pid x) (send-recv pid))))

If you’re a Schemer, you’ll readily understand the code above, which uses the same primitives as Erlang: !, recv (since receive is taken in Gambit) and spawn.

What about the comparison? Well, i love Scheme and, to my biased eyes, Gambit’s version is more beautiful. So let’s move to something that is not in the eye of the beholder: speed. Running ring:make_ring(5000, 1000) in my laptop gives the following output:

Total time=970000 (1520000) microseconds

while typing (make-ring 5000 1000) at the Gambit’s interpreter prompt yields:

CPU time: 19.442507999999997 secs
Real time: 19.780327081680298 secs

So, the Scheme interpreter is about 20 times slower than the Erlang interpreter (the factor seems to be more or less constant when you change the number of processes or messages sent). But Gambit has a secret weapon: it’s a Scheme to C compiler (why, it’s actually called Gambit-C!). So I compiled my program and ran it, with the following result:

CPU time: 2.3031090000000005 secs
Real time: 2.3484270572662354 secs

The interpreted Erlang code is still more than twice quicker than the compiled C code. Not bad: the more i look at that Erlang program, the more beautiful it looks!

Of course, this is just a toy benchmark which says nearly nothing about the real performance of either Erlang or Termite. I just thought it could be a way of whetting your appetite and get you started in these to systems that i’m just discovering. Googling for Erlang and Termite will direct you to several recent blog entries discussing them and helping you to join the fun, but, lest you miss it, let me recommend this Termite mini-tutorial by Marc Feeley.

And again, if you discover any of the silly things i could do better in the programs above, please leave a comment: i’m just learning!

Three questions

April 29, 2007 by jao

Richard Hamming’s three questions for new hires at Bell Labs:

  1. What are you working on?
  2. What’s the most important open problem in your area?
  3. Why aren’t they the same?

(from R. Hamming’s You and your research. Essential reading.)

Sven’s movies

April 21, 2007 by jao

Sven Van Caekenberghe is the author of some interesting Common Lisp hacks, including KPAX, a nice (web) application framework, and cl-prevalence, which is the one that drove me to his homepage. And there i found his Lisp Movies.

Bets are seasoned common lispers will be well aware of them (the screencasts are more than a year old), but new CL aficionados or anyone wanting to see the much touted dynamism of Lisp in action will find in this couple of movies a good example of how it feels programming web applications within a really powerful environment. Sven interacts in this two-episode series with LispWorks to create a simple HTTP server (and modify it on the fly) and to write a Reddit clone in 20 minutes and 100 lines. The latter uses KPAX, so, as a bonus, you’ll get a mini-tutorial on the framework.

But of course the nicest bit is seeing how one starts with an empty but running server and modifies or, rather, debugs the program until it behaves properly. All that keeping it alive and using the tools provided by the environment (inspectors, listener, debugger) to bend and evolve the code. Ah, the Joy of REPL once again.

Easter egg

April 9, 2007 by jao

After some years playing with other implementations, i’ve been using the PLT-Scheme suite assiduously during the last weeks (more on what for in future posts), and it’s becoming, slowly but surely, my default implementation. Eli Barzilay’s extensions to make MzScheme play nice with Emacs were the trigger, but i must say that DrScheme’s macro stepper, debugger and syntax checker (all of which i had not seriously used before) are fine pieces of hackery. If you’re into scheme, you really should take a look at them. Another strong point of PLT scheme is its excellent documentation, which comes with a browser called HelpDesk. I use it all the time, and this morning it saluted me with an unexpected background:

200704090100

Let me join the toast, and wish Shriram a happy birthday too! (and, while i’m at it, thanks also for your wonderful PLAI). It must be nice being a PLTer, mustn’t it?

Another Glitch in the Call

March 17, 2007 by jao

Another Glitch in the Call
(Sung to the tune of a similar Pink Floyd song.)
(Contributed By Knappy 8350428 @ UWAVM)

We don’t need no indirection
We don’t need no flow control
No data typing or declarations
Hey! You! Leave those lists alone!

Chorus:
All in all, it’s just a pure-LISP function call.

We don’t need no side effect-ing
We don’t need no scope control
No global variables for execution
Hey! You! Leave those args alone!

(Chorus)

We don’t need no allocation
We don’t need no special nodes
No dark bit-flipping in the functions
Hey! You! Leave those bits alone!

(Chorus)

We don’t need no compilation
We don’t need no load control
No link edit for external bindings
Hey! You! Leave that source alone!

(Chorus, and repeat)

Optical input methods

March 2, 2007 by jao

n the last issue of MIT’s Technology Review, there’s an interesting report, An Alternative to the Computer Mouse on Manu Kumar’s new way to interact with your computer via eye-sight. The innovative part comes in the combination of eye and hand: you fix your stare on a given screen region, but that has no effect unless you press the appropriate key. Once you do that, magic happens. For instance, the region you were looking at gets amplified. Or if you were reading a text near the end of the visible area, it will scroll automatically for you. There’s no secret hardware involved (a high-resolution web cam and a bunch of infrared light-emitting diodes), although that does not mean that the equipment is cheap: Kumar uses a $25,000 monitor! The tricky part happens to be a good eye-tracking algorithm, because it seems pupils are prone to jitter even when fixated on a point. Kumar’s solution is not yet perfect (tests show that it misbehaves around 20% of the times), but people participating in tests seem to prefer eyes to mouses. The system is called EyePoint, and you’ll find some screenshots here or, download a demo video here. The project is integrated in Standford HCI Group’s GUIDe (Gaze-enhanced User Interface Design, if you have to know), which includes other curious applications like EyeExposé (i’ll let you try and imagine what it does).

Although not as fancy as other exotic input methods we’ve seen in the past, this one looks much more practical: i can perfectly picture myself using EyePoint together with my keyboard and Emacs to browse around during code editing sessions. I’ll have to wait until prices drop a little bit, and algorithms get better, but that should be only a question of time.

Scheme loops

February 25, 2007 by jao

If you’re a schemer, expressing iteration by means of tail-recursion and named lets has probably become second-nature to you. I find this idiom both natural and elegant, but as Olin Shivers points out in his DanFest presentation below, it has some drawbacks. For instance, the code in a typical named let will work only for a given sequence type (e.g., a vector or a list); and, as Olin explains, its ‘goto-nature’ does not mix well with lexical scope. In other words, tail-recursive schemy iteration is too low-level and, according to Olin, a higher-level abstraction hiding details and providing a modular interface and proper lexical scope is called for. In his presentation, he outlines his solution to this problem, which, as you will see, amounts to defining a looping domain-specific language (actually, a couple of them). A more detailed explanation is given in his paper The Anatomy of a Loop. As an extra, the first few minutes of the video below will get you acquainted with the mysterious  Dan effect.

But Olin’s is not the only solution to looping in scheme. Alex Shinn wrote and excellent survey on Scheme iteration methods in his Yow! LOOP macros are LOOPY!, which discusses at length current approaches to solve the problems mentioned above (like srfi-42 eager comprehensions or Jonathan Amsterdam’s iterate macro). For extra fun, Alex includes implementations for most of the loop constructs he reviews, a good way of improving your macro programming.

(Also worth reading in this context are Oleg Kiselyov’s thoughts on collection traversal APIs, where he draws a clear distinction between enumerators (such as scheme’s for-each or fold) and cursors (such as C++ iterators, i.e., objects maintaining internally the “current element” of the collection being traversed). As you will see, he makes a pretty good case for enumerators.)

So, you see, there’s many a way to loop a cat!

Classic Texts in Computer Science

February 22, 2007 by jao

Babar Kazar has compiled an awesome (and i mean awesome) list of Classic Texts in Computer Science. There one finds almost every paper to read if one is serious about computers. The list is too long to put it all here, and virtually all of them are interesting in one way or the other, so i won’t make a selection right now: just take a look at them, and try to pick one: i bet you’ll have a hard time deciding!

Although it’s a bit late for new year’s resolutions, here is mine: i plan to at least skim through all these texts, read many of them, and, of course, keep you informed.

Update: Babar recently updated his site, and i have updated the link above accordingly: here’s your chance if you missed this post the first time.  He has also  some other online stuff worth a look.

A (video) celebration

February 21, 2007 by jao

On December 3 and 4 of 2004, the Computer Science Department at Indiana University hosted a conference on the occasion of Dan Friedman’s sixtieth birthday. It brought together many of his former and present students, colleagues, research collaborators, co-authors and friends. That is, it brought together many of the big names in the Lisp/Scheme community (not in vain Shriram Krishnamurthi once imagined the Scheme community “a brotherhood of Dan”). Guy Steele delivered a one hour keynote, which was followed by more than twenty half-an-hour talks by people like Sussman, Dybvig, Shivers or Kiselyov, just to name a few.

The extremely good news is that, in the conference’s webpage, one can find the videos for almost all these talks! So, don’t be surprised if i’m missing during a few days! Besides the obvious interest in their contents, i’ll also find amusing to connect names with faces (it’s funny how one makes up fictitious faces for the authors of papers one reads and re-reads)… how many of them do you recognise?:

(I admire the work of many of these people, and i was convinced i wouldn’t be able to single out one among them… but to my surprise, and with all due respect to all schemers, i can: to my amusement, one of the participants is one of my few personal heroes. I’m not sure about the connection between him and Dan, but i’m very pleased to know there’s one. Surely you can spot this (to me, at least) rara avis?)