Destructors

July 27th, 2008

I think that of all the features of C++, destructors are probably my favorite. They seem simple enough (code that is run when an object is destroyed, ensuring resources are cleaned up) but when you look into the consequences of that, they change the entire game and mean that C++, while it looks at first glance quite similar to C, is a very different language altogether.

RAII gets talked about a lot but the real meat of it is RRID - the destruction part.

For one thing, they mean that some things can now happen implicitly, rather than having to be explicitly coded. That's a good thing - it means the higher-level code can be terser and terser code is less likely to contain bugs. A common complaint of C programmers learning C++ for the first time is that they can't see exactly what's going on by looking at the code any more - but what they haven't yet realized is that that is a good thing.

For another thing, they make exceptions possible, with all the benefits they bring, and without the overhead, non-orthogonality and non-determinism of garbage collection.

Because destructors are not like normal functions, there are some things that you should not do with them (like throw exceptions from them). One way to think about this is that normal functions (including constructors) do "forward" work and destructors to "reverse" work (undoing things, releasing resources etc.). The reverse work should never be able to fail because that means that neither forward progress nor reverse progress can be made - it means the program is irretrievably broken and must be terminated immediately. Hacks like uncaught_exception() should not be used - if you're tempted to use it is a sign that you're trying to make forward progress in a destructor and that you should re-think your design.

Another way of writing "reverse direction" code in some languages is "finally". It's too bad that C++ doesn't have this (you either have to catch and rethrow or write a special object) - there are occasions when you want to do some one-off cleanup and don't want to have to write a whole class just to get a destructor, and the "finally" notation makes it clearer than the catch/rethrow notation. I wouldn't give up destructors for finally, though, as some languages do.

Homogeneous computer

July 26th, 2008

I was thinking about FPGAs recently, and that got me wondering about the possibility of designing a computer architecture that is completely homogeneous - that is, composed of a number of identical cells arranged in a square grid. Some of the cells (presumably those on the edges or even just a corner or two) would be connected to IO devices (perhaps directly, perhaps via a bus for a more general machine).

Each cell would have some kind of logic and a small amount of memory (enough to remember how it is connected to adjacent cells and to use as actual user memory).

We would like to be able to have different pieces of the machine doing different things at the same time, so cells should be independent (except where we explicitly connect it to an adjacent cell).

We also want to be able to have the hardware reconfigure itself (so while group-of-cells-A is doing something else, group-of-cells-B can be reprogramming group-of-cells-C). In order to do this, we need to make it possible for each cell to be able to address any other cell that its connected to.

If we have some string of cells connected together in a line, how do we differentiate between data sent all the way to the end of the line and programming instructions sent to a particular cell somewhere along the line? For a chip with 2n cells we need to have at least n+1 bits connecting each cell to its neighbours - n data bits (x) and one "program" bit (p). If a cell in "interconnect" mode sees an input of (p,x)==(0,x) then it passes (0,x) out the other side. If it sees (1,x>0) then it passes (1,x-1) out and if it sees (1,0) then it is reprogrammed. I guess we'd also need some additional data bits to tell the cell what mode it should be in after reprogramming. A cell with 4m input bits and m output bits has requires 24mm bits to completely describe its truth table and this is clearly going to be larger than m so obviously we can't program it with any arbitrary truth table in one go - we'd either have to upload it in pieces or limit the possible cell modes to some useful subset of the possible truth tables.

This is probably an incredibly inefficient way to build a computer, since for any given state a large number of the cells are likely to be acting as just interconnects or just memory and only using a tiny proportion of their capacity. But having the cells be identical might mean that doesn't matter so much if we can build a very large number of them very cheaply.

A variation on this architecture might be to have the cells arranged three-dimensionally in a cube rather than than on a flat 2D surface. Current photolithographic fabrication technologies aren't quite up to this yet (the fattest chips are only maybe 10 layers thick) but I expect that the nanoassembly and microfluidic channels for integrated cooling that will be required to make fully 3D chips aren't too far off. When that happens the chips will be so complex that we'll have to use concepts like repetition and fractal-like scale invariance to a greater extent to make their designs tractable. In other words, chips will start to look more biological - more like living tissue. Perhaps a more homogeneous computer design will come into its own in that era.

Concrete data type

July 25th, 2008

A handy thing to have around (and something I want to put into the UnityALFE language) is a data type for representing physical quantities. This keeps track of a real number plus powers of metres, seconds, kilograms, amps, Kelvins (and maybe others, including user-defined ones). Two values of type Concrete can always be multiplied or divided but can only be added or subtracted if their dimensions match, and can only be converted to other types if they are dimensionless.

Common units of measurement and physical constants can be given as constants. Because the units are tracked automatically you can do things like:

(2*metre+5*inch)/metre

and get the right answer.

Usually the compiler should be able to check the dimensions at compile time and elide them like other type information, or give a compile error for expressions like:

(2*metre+5)/metre

Along similar lines, the log() function could be considered to return a value of type Concrete with some special non-zero dimension. Then you can (and indeed must) specify to which base the logarithm should be by dividing by another logarithm (e.g. log(x)/log(e), log(x)/log(10) or log(x)/log(2)). This syntax is rather more verbose than the usual method of having separate functions for common bases (log(), lg(), ln() etc.) but I find that this is more than made up for by the fact that one doesn't have to remember which function corresponds to which base - it's self-describing.

Another useful type for scientific/engineering work would be a value with confidence interval (e.g. 5±1 meaning "distributed normally with a mean of 5 and a standard deviation of 1"). There are well-defined rules for doing arithmetic with these. A generalization of this to other distribution functions might also be useful.

Twenty years of programming

July 24th, 2008

I recently came across an archive of old files that I hadn't looked at in a while, and found what I think are the first programs I ever wrote. In particular, the oldest program I can find that I'm sure I wrote all by myself (that wasn't written by a relative or typed in from a book) is dated 24th July 1988, 5:23pm. It's possible that I started it earlier than that or that other programs written at the same time predate it - the only timestamp supported by the DOS filing system was "last modified".

"More or Less" was written in Locomotive BASIC2 which ran under GEM on our Amstrad PC1512. GEM and BASIC2 (and therefore "More or Less") can still be run on a modern PC with the help of the instructions here and DOSBox (when run under Windows XP the instruction pointer seems to end up pointing to non-code data for some reason, and Vista graphics drivers don't support graphical DOS programs).

"More or Less" is a rough clone of a piece of educational software of the same name that ran on the BBC micros at school. I found a copy of the original - the copyright information says:
MLESS / More or Less
Microelectronics Education Programme
MICROPRIMER Software Pack 4
(c) CET 1982 /ISBN 0-86184-085-2
Program idea: A Straker, I Drysdale, A J Curtis
Programmed at C.E.C.C.
Editor: Bob Coates & Five Ways Software
Version 1.0 / 1 November 1982
Works with BBC Micro 32k (480Z and SPECTRUM versions also available)

The program displays two numbers and you have to say whether the left one is more than, less than or the same as the right one.

Not particularly challenging (even for 9 year olds) but I think the BBC version at least kept track of how long you took. I intended to add more features but (like so many of my programs since) I never got around to finishing it.

Another interesting feature of the BBC version was the intro/title screen. This printed "More or Less" in letters that grew and shrunk in height so that the line formed by the tops of the letters bulged in and out:

(kind of a primitive version of texture mapping I suppose). I wanted to reproduce this effect, but my programming skills were not up to it at the time. But I recall being quite proud of what I came up with instead - the title was centered, printed in a large font, double-spaced and the letters appeared one by one, each one accompanied by a beep (without a third party driver that I wouldn't come across for several years, BASIC2 was only capable of generating that one sound programmatically).

My version did have a few interesting features - it asked for the player's name and addressed him/her by name for right and wrong answers. The program maintained a table of scores in a disk file (not sorted, though, and not necessarily "high scores" - I didn't know about arrays at that point).

I'm sure I had written some other programs around the same time or earlier, but I guess I wasn't sufficiently proud of them to save them. In my last year at Goring primary school (which ended around the same time I wrote "More or Less") I also wrote some Logo programs to draw letters of the alphabet with turtle graphics. These were not particularly sophisticated (just sequences of forward and turn instructions, plus loops for the curved bits) but were probably the first actual programs I wrote. I don't have any record of them, though.

My first made-up language

July 23rd, 2008

Years ago (more than a dozen), I tried to write (in C) an interpreter/compiler for a dialect of BASIC that I made up. It was based on BBC BASIC and the BBC's graphics facilities (which I was very familiar with at the time) but it would have run on the PC and had some additional features:

  • Operators and control structures from C
  • More string and array processing facilities
  • More graphics commands
  • Interactive editor and debugger
  • Commands for accessing PC facilities (interrupts, calling native code etc.)
  • Built-in help
  • Facilities for storing chunks of code as a library
  • Complex numbers
  • Self-modification and introspection facilities

The last of these is what I want to write about today.

As a child I used many of the 8-bit BASIC variants which had line numbers and it always irked me that there were some things you could do in immediate mode that you couldn't do from a running program, such as editing the program. Why was it that typing:

PRINT "HELLO"

printed "HELLO" immediately and:

10 PRINT "HELLO"

printed "HELLO" when the program was run but

10 10 PRINT "HELLO"

didn't create a program that replaced itself with the program '10 PRINT "HELLO"' when it was run? While doing so didn't seem terribly useful it seemed to me that an interpreter would be just as easy (if not easier) to write with this facility than without it, and that it was an unnatural ommission.

Along similar lines, my dialect had an "INTERPRET" command which took a string and ran it as BASIC code and a "PROGRAM$" array which contained the lines of the program.

I got some way in to writing a parser for it but as I didn't know how to write a parser back then I got horribly tangled trying to write code for each possible combination of current state and next piece of input.

The similarities between this and the UnityALFE language that I'm in the (very slow and gradual) process of writing haven't escaped me.

Boxes

July 22nd, 2008

I recently reorganized a big box of junk and put into smaller various boxes. Behold my amazing junk classification scheme:

  • Broken electronics, for spare parts or recycling
  • Things that work but which I don't currently have a use for
  • Spare Ikea parts
  • Ikea tools
  • Stuff that I don't know what it is.
  • String
  • Nails
  • Screws
  • Nuts and washers
  • Screw eyes and pins
  • Stickers
  • Picture hooks
  • Screw hooks
  • Rawl plugs (or wall anchors as they are called here)
  • Anti-static bags
  • Small plastic toys (I'm sure Alexander will have a lot of fun with this box when he's old enough not to swallow the parts)
  • Junk (stuff that doesn't fit into any of the other categories)

It occurs to me that the category system on my blog works a similar way (though the category corresponding to "junk" is called "random").

Photos - January to May 2008

July 21st, 2008


New car




Office/basement before repainting. See here for the after pictures.


Bath time




The red jumper that Alex's Nana made for him.


Painting the guest room

Before:

After:


Pasadena, February:


Our gracious hostess, Sarah


Mum & Dad's visit, March:


At Seward park


First photo taken by Alex



Easter


In the garden


At the Olympic sculpture park

Temporal or non-temporal blogging?

July 20th, 2008

I have a 70 day blog backlog (at time of writing, 44 at time of posting), meaning that even if I don't write anything for 6 weeks you'll still get a post every day before I run out. So far I have been operating this backlog as a FIFO queue which seems to work pretty well. The only problem is, though, that now if I write something it might be quite out-of-date by the time it's posted. This discourages me from writing about current events. Perhaps I should let the backlog drain (by posting faster than I write if necessary) and then post as articles come into my head like most people do.

Cool stuff update

July 19th, 2008

Cool Stuff You Can't Find On The Web has been updated:

...there used to be a magazine (I think) which came with a tape which had various childrens stories on it. The one I remember was "The thin king and the fat cook" but there were a few on each tape. I had a couple of tapes, but I've lost them now. Maybe they are still at my parents' house somewhere.

[Update!] - This is what I was talking about. Apparently I had "Story Teller 2" parts 5 and 16 and possibly also "Christmas Story Teller" part 2, because the titles Bored Brenda, Noggin And the Birds, The Snow Bear, The Inn Of Donkeys, Shorty The Satellite And The Brigadier, The Nightingale, Hugo And the Man Who Stole Colours, Mole's Winter Welcome, The Tale of the Little Pine Tree and Grogre and the Giant Nasher seem familiar. I remember very little about any of these except that (as I recall) some of them made me feel quite sad. And there was something about a picnic of bread, cheese and apples in one of them. And people getting swallowed up by a bog. Derek Jacobi's voice still makes me think of these stories to this day. It's quite possible that at least some of these tapes were chewed up by my tape player - it used to do that every once in a while (particularly when I stuck things into it - I was a little scientist).

[Update!] - I got a hold of a digital copy of all of the tapes and magazines, and they are just as good as I remember - extremely well done. I have been playing them for Alexander but he’s a bit young for them at the moment. I look forward to the day when he is old enough to enjoy them. The one with the picnic was “The Snow Bear”, and the sad one was “The Nightingale” (all these stories have happy endings, though). “Mole’s Winter Welcome” still brings a tear to my eye.

There's something very surreal and very wonderful about finding something you remember from a very long time ago in early childhood, and having memories of that time come flooding back. This has happened to me a few times now (mostly because of the internet). Sometimes they things you remember are even as good as you remember them being.

That happened to some extent when I watched The Mysterious Cities of Gold again at university. Some parts (like the butterflies on the approach to the New World, Tao's sadness at the destruction of the Solaris and the first time the Golden Condor flies) were as magnificant as I remember, but some bits seem a bit implausible now and I had forgotten just how destructive those kids were - just about every magnificent ancient treasure and building gets turned to rubble in their wake!

Speaking of MCOG - Oh wow, I just noticed that they're making a movie of it - awesome!

Why carbon credits are not like Catholic indulgences

July 18th, 2008

A few times now I've heard the meme comparing carbon offset credits to Catholic indulgences - where one can "buy one's way out of punishment". I think this argument is completely bogus and that the similarities are superficial at best.

Carbon offsetting is an economic solution to an economic problem. Many people want to live a more green life but don't want to change their habits to do so - many of the things that they want to buy or do (like air travel) don't have (more expensive) carbon-neutral equivalents. Perhaps such equivalents are possible but the demand isn't sufficient yet for them to be practical. But just like the invention of money made trades possible that were impractical with the barter system, carbon credits make it possible for some people to be carbon neutral without everybody having to be so at once.

The key here is that the aim of the exercise is to reduce the total amount of carbon dioxide in the atmosphere. Doing something which adds CO2 and something else which removes it is just as good as doing neither.

Sin, forgiveness and punishment don't work like that, though - doing one good deed doesn't (and shouldn't) make up for an unrelated sin of equivalent value, because (unlike CO2) the total amount of sin isn't a meaningful concept - everyone just cares about the sins that are made against them in particular.

Also, Carbon credits can have a measurable effect whereas it's impossible to be sure just how long you're going to spend in purgatory. So fraud is more difficult (though still possible - we do need to have some way of ensuring that the money spent on Carbon credits is actually being spent on what it is being purported to instead of being squandered and that that activity is having the desired effect).