[erlang-questions] Santa likes Erlang better than Haskell
ok
ok@REDACTED
Wed Mar 14 00:59:15 CET 2007
Yesterday a colleague told me about a draft chapter (for a book called
"Beautiful code", editor by Greg Wilson) written by Simon Peyton Jones.
The chapter is about Software Transactional Memory in Haskell.
You'll find it through http://lambda-the-ultimate.org/node/1964
Software Transactional Memory has already been mentioned before in
this mailing list,
and the chapter explains the basic ideas clearly enough. As I read,
however, I felt
a bit worried. STM makes it easy to write CORRECT programs, true
enough. (But see
below.) But with your transactions being aborted and restarted
whenever there is a
conflict, it seems to me that it is possible that there might be very
large overheads
without the programmer having any way of telling that this is happening.
Something else bothered me. I've been using Haskell for years, even
teaching it, but
I found the code in the chapter difficult. I could understand each
piece, but it was
totally unclear to me how you would design something like this. Key
concepts, like
the Gate data type, came out of the blue. There are things like
lists of promises
to return promises to perform actions. Did the problem really demand
a solution
this complex?
The sample application in the chapter is the Santa Claus problem.
Santa has 9
reindeer and 10 elves. Whenever the 9 reindeer are ready, Santa goes
delivering
toys with them. Whenever any 3 elves want a meeting, provided the
reindeer are not
waiting, Santa has a meeting with them.
Haskell solution: 77 SLOC.
Erlang solution: 46 SLOC.
Haskell techniques: too many to list.
Erlang techniques: one process for Santa, one for each elf, one for
each reindeer,
plus two secretaries (one waits for 9 reindeer
and forwards them
as a group to Santa, the other waits for 3 elves
and forwards
them as a group to Santa). Two nested receives
in Santa to get
the desired priority effect.
Haskell hidden cost:transaction abort/restart
Erlang hidden cost: well, none, really. Santa's mailbox can have at
most
1 reindeer group, 3 elf groups, and 9 good-bye messages;
one secretrary's can have at most 9 requests and
the other's
at most 10; and each reindeer or elf can have at most one
proceed message.
It makes me really pleased to know Erlang, and rather worried that
Simon Peyton Jones
thinks his 66% larger and far harder to understand solution is
"beautiful".
More information about the erlang-questions
mailing list