Wednesday, 18 February 2009

Erlang Text Elite

This last weekend, I somehow came to think about the wonderful old space trading game Elite, and the way it managed to cram a vast universe of planets with names, stats, and fun descriptions (along with the 3D space flight simulation, which was an amazing feat in itself) into the tiny 8-bit home computers of old. Only minutes later, I had found Ian Bell's Text Elite web page, where Ian, one of the two original authors of the game, has published a reconstruction in C of the main universe-generating algorithm, along with a simple text interface to actually let you "play", i.e., buy, sell, and jump between planets. Great!

So I rewrote it in Erlang. And had a lot of fun doing it. Apart from the nostalgic kick, there were two points with the exercise: First of all, I just wanted to see the main algorithm as cleanly implemented as possible. But second, I wanted to see what it would be like to rewrite a piece of classic game code that was very clearly not written with functional programming in mind (though of course already cleaned up a bit by being rewritten in C from the original 6502 assembler). I think I like how the result turned out; it's quite clean and readable, and the data flows are easy to follow. I also found and fixed a bug in the "goat soup" function.

The code can be found on GitHub, along with the two scripts for testing found on Ian's page. You can run them like this:
erl -noshell -s txtelite main -s init stop < sinclair.txt
(To play yourself, just skip the last "< sinclair.txt" part.) Of course, you need to compile the txtelite.erl file first. If you're new to Erlang, this is how:
erlc txtelite.erl
It's good to be back on Lave again!

Monday, 9 February 2009


Lately (well, for almost a year now), I've been watching a whole lot of the British TV series "Time Team". If you've never heard about it, it's a show about archaeology. Before you fall asleep, let me assure you that it is as far removed as possible from any boring documentaries you have may watched (you know, the ones with a voice that drones on and on about some kings and battles with a slideshow of still pictures for illustration). No, this is a show about digging. Also, the fact that it's been running since 1994 should tell you something of its popularity.

The idea of the show is to follow a "crack team" of professional archaeologists over three days, as they attack a particular site of interest. The sites are chosen based on various sources, from previously known places where some finds have been made before (but no proper investigation has been done), to the back garden of some family who found something strange when they were buring their cat. Sometimes the target is ancient, even prehistoric, and sometimes it can be from the WWII era. There is a huge variation in material covered, and although not every episode can be spectacular (but many are!), out of some 150+ regular episodes so far, they tend to range from fairly interesting to jaw-droppingly incredible.

In my mind, it is possibly the ideal TV program; an optimal use of the medium, and immensely entertaining, educating, and enlightening. But it is also simply a lot of fun, with much banter and fun-poking going on between people who combine vast amounts of knowledge with sharp wit in that relaxed manner of the British. In fact, probably no other country could succeed with such a show, without either turning it into an acedemic pissing contest (consider the French), a testosteron-filled infotainment show (the Americans), or just a bleak and humourless educational program (the Germans, or the Swedes for that matter). The Brits, on the other hand, will stay focused on getting the facts, but at the same time remain relaxed and optimistic, tease each other a lot, and continue the discussion over a pint down at the pub.

Of course, Britain is also an ideal country for archaeology, with its multilayered history of Celts, Romans, Saxons, Vikings, Normands, knights and castles, Tudor mansions, catholics and protestants, civil war, industrial revolution, Georgian manors, WWII fortifications, and whatnot. There are also lots of old documents, including what is known from Roman sources during the occupation, the Anglo-Saxon Chronicle that refers to events as far back as 60 BC, and the important Domesday Book survey of England from 1086. In comparison, we hardly have any solid facts at all about Sweden before 1100, much less a complete list of who owned what.

The program host, who explains what is going on but also functions as the viewers' representative who runs around and asks all the silly questions, is Tony Robinson, also known as Baldrick of the 1980s comedy series Blackadder, and he is perfect for the job. Like Watson to Sherlock Holmes, he is no fool, but without him the audience would not have any way to follow what is going on. It is probably safe to say that the show would not have been a success without him, the way he alternates between radiating enthusiasm for the subject and the potential discoveries at hand, and expressing dismay at the lack of interesting finds and slagging off the archaeologists when all they have found is a few ancient post-holes and stones - which of course forces the archaeologists to explain exactly why these finds really are interesting and what conclusions you can draw from them, which was the whole point.

But it's also the people themselves that make the program so much fun. Apart from Tony Robinson, we have:
  • Professor Mick Aston, the team captain and benevolent dictator, with all the dress sense of a... well, of a British archaology professor who likes multicoloured knitwear. Always cheerfully optimistic even when it's the afternoon of the last day, they've found almost nothing, and a storm is approaching.
  • The inimitable Phil Harding with his hat, wild hair and whiskers, and a West Country accent which could be called "broad" in the same way that the Chinese Wall could be called "long". Never happier than with a shovel or trowel in hand, unless it's a piece of flint, or even better, a beer. Frequent exclamations of "ooh, ah!", "ooh, look at that!", and the occasional "stone the crows!"
  • Stewart Ainsworth, archaeological surveyor. While the rest of the team are frantically working away at their trenches and theories, he wanders around the landscape for a couple of days with maps in hand, bobbing his head up and down and occasionally lying down flat on the ground to get a better look at the "lumps and bumps", goes to the library to look at some ancient maps, and then returns to the others to tell them they've got it all wrong. Most of the time he's right.
  • John Gater, geophysicist, who heads the "geofizz" team. In early episodes, when asked about some splodge on the survey results he'd refuse to say more than "I don't know, it's an anomaly". These days, he doesn't hesitate to state "look at these responses, that must be a roman temple", and is frequently proven wrong when he does so, but it's much more fun that way.
  • Various other regulars, some of which have moved on over the years, such as Carenza Lewis, "Mick the Dig", and flamboyantly dressed historian Robin Bush.
  • Semi-regulars who are called in depending on their areas of expertise, like the deliciously named Guy de la Bédoyère who can date a Roman coin at twenty paces.
I don't know if it's just me, but at least I find it immensely uplifting and entertaining to watch a TV show full of people who can read and simultaneously translate original manuscripts in Old English and latin; people who can pick up a literally thumb-nail sized piece of broken pottery and almost instantly tell you what shape the original pot would have been and also that it was made in a particular town in Surrey around 1260 AD, by a left-handed potter in his late 40s; people who can look at a piece of a stone that was once part of a building and not only tell you which part, but also at what date, where the stone came from, and probably the name of the mason; bone specialists (osteoarchaeologists) who tend to get a slighty kinky gleam in their eyes whenever they find a skeleton (or more usually, pieces thereof), and how much they can tell from it; and "small finds" experts who can identify and date various small items based on changes in fashion and manufacturing techniques over the centuries.

What it all does, I think, is that it so well answers that question that everyone from schoolchildren to politicians ask on a regular basis: "but why should anyone want to learn that, and what good is that sort of research?" It shows that even if it can seem dreary to learn all the basics of a huge subject (that perhaps nobody else you know cares about), there may come a day when you are part of a group, and your deep knowledge about that particular subject can turn out to be really useful, even crucial. True, if everyone was an expert on coproliths, conversation might get pretty dull after a while. The cool things happen when you put people together who are experts in different (though adjacent) areas to solve a common problem. If you are really good at something, interesting tasks will find you, not the other way around.

Perhaps even more important, the show also demonstrates very clearly (and completely without being boring) the principles of scientific thinking and method, and why it is necessary. And mostly, this just happens as a side effect. Often, some impatient person will wonder why on earth they need to do things so carefully and in a specific order (this someone is often Tony Robinson who wants to see some action, or some happy amateur archaeologist who was there a century ago and came to some fairly premature conclusions). However, as the digging proceeds, it is often clear that it is because of the methodology that they can draw the strongest conclusions, and sometimes it forces them to throw away all previous theories. Or that in some cases, the normal methods just don't work, so you have to try another method, and another, or even improvise a new method, whatever works to solve the problem. It's what science is all about, turned into a whodunnit.

It also shows why a theory is only a theory until it has been thoroughly tested. No matter how clever and knowledgeable the person who proposed the theory, or how much the clues all seem to point in the same direction, the theory can still be wrong. If you can't provide some solid support for it, you must be honest and admit that for now, it's just a conjecture. An expert may get it right most of the time (much more often than you or me), but can still be spectacularly wrong sometimes, and testing is the only way to find out. Or as their buildings expert admitted in a recent episode from the 2008 series, he had been "slapped round the face by the wet haddock of reality". Makes me wish this was obligatory viewing in schools everywhere.

Watching the episodes in chronological order adds an extra dimension, not only because it follows the core team members over 15 years, which is fun in itself, but because it fast-forwards through some major advances in technology. When the first episode aired in 1994, the World Wide Web was quite new, and being able to display a map on a computer screen and draw some lines on it was considered high-tech. So was texture-mapped polygons - Doom was published in 1993 - and showing a primitive model of some old castle with the camera rotating around it was very flash indeed. Following the show, we get to see how computer modelling steadily improves, and GPS becomes not only a standard technique for geophysics, but so precise that you can measure key points with centimeter precision. Then bring in some elevation data from a national database, and suddenly you can show a rotating 3D-model of the entire landscape, with a superimposed map from the 16th century and a great little textured and shaded model of a Tudor mansion on top, and no-one bats an eyelid. A lot has happened in this decade-and- a-half.

Sadly, there is no "complete collection", not even a single entire series, to be found on DVD - if there was, I'd buy it immedately - but people out there have been recording it and digitizing it for the good of mankind. You probably know where to look.

Sunday, 1 February 2009

The times they are a-changin'

Recently I got a call from my dad, who was having some problems with his computer, along the lines of "there's something wrong with the damn internet again". Since I was the one who set it up for him, I don't mind fixing problems when they occur, but I find that the hardest thing with these debugging sessions over the phone is to get him to quote the exact messages he is seeing. It's kind of interesting, as if the words and concepts are so alien to him that he zones out after only a second, and is actually unable to read them aloud. A typical exchange goes like this: -So, dad, what does the error message say? -Oh, something about the Internet. -OK, but what does the text say, precisely? -I dunno, Microsoft and Internet something. -Dad, can you please tell me exactly what the text says? -I just told you, it says it can't find the Internet. -Where is the message, I mean, what program does it come from? -Well, dammit, it's right here, on the screen!

And so on.

This time, though, the problem turned out to be kind of funny. He really needed to log in to his online bank, and it was important, so although the symptoms sounded as if it was a problem with the remote site (something about an invalid certificate), I promised to take a closer look. Last time I was at their place, I made sure that remote desktop was enabled, and that their router would forward the right ports, so it would be easier to handle these situations. However, I still needed the IP of their router, so I had to spend some time voice-guiding him step by step through the router's web interface until he could quote me the address. Armed with this, it was no problem to log in. (I had the administrator password already.)

It turned out that he was absolutely right, the bank (and any other secure site he had tried) would complain about the certificate being outdated. But it was outdated in an unusual way: "this certificate will not be valid until ...". For a couple of seconds, I stared blankly at the screen, and then I moved the mouse down to the right corner and hovered over the clock widget. It said it was January 2003.

So the clock had reset at some point (I knew that the BIOS had had some hiccups lately), but for some reason the automatic clock synchronization didn't correct it. I checked, and it was enabled and it was pointing to a good NTP server. So I tried to force a new synchronization. The result: a message saying that the local time was so much off that it wouldn't even try. Well, thanks for nothing: in a situation where you really need to be saved by an external time source, it simply won't do it. There might be some good technical reason for that, but I can't think of one right now, so I think about punching Steve Ballmer instead. (Mmm...)

Anyhow, this just shows again how hard it is to foresee what kind of problems might occur with a setup once you leave it on its own for some time. No matter how well you've configured it so that nothing can possibly go wrong, something will eventually find a way to go wrong, and if you're particularly unlucky, it will be in such a way that the symptoms are quite far removed from the cause: in this case, a hardware glitch that reset the clock had no apparent effects, except that dad was suddenly not allowed to log on to his internet bank.

As long as this kind of thing can happen (and much too easily), how can we ever hope to give grandma a computer that "just works" and that won't suddenly barf up some weird messages on the screen and transform into an expensive brick?