Tuesday, March 30, 2010

Tool for generating algebraic types in C

I've spent a few evenings crafting a tool to automatically generate C code implementing algebraic data types (or discriminated union types) from short specification files. For example, the following specification
@ type a_b_tree
@ constructor a
@ constructor b
@ constructor branch
l : a_b_tree *
r : a_b_tree *
    if (l == NULL || r == NULL) {
        fprintf(stderr, "a_b_tree: branch cannot have NULL arguments.\n");
@ end
expands to 144 lines of C (counting the .h and .c files).

The transformation supports
  • generation of constructor, deconstructor, and freeing functions;
  • optimised handling of constructors with no arguments (memory is not allocated for separate instances of these);
  • optimised handling of types with only a single constructor (there is no need in this case to distinguish between the type and its constructor);
  • user specifiable malloc and free functions;
  • user specifiable header and footer code for the generated .h and .c files.
You can download this tool here.

Thursday, March 11, 2010

The anomaly method and UHI

This is rather interesting. It seems the anomaly method of only considering digressions from the mean of a temperature series (i.e., the series' anomalies) does account for the urban heat island effect (i.e., it effectively removes the unwanted UHI component from the signal).

Thursday, March 04, 2010


This is witty and helpful.

I particularly like
  • Don't string too many prepositional phrases together unless you are walking through the valley of the shadow of death.

Monday, March 01, 2010

Cute "proof" of Pythagoras' theorem

[For reasons I have yet to fathom, the ASCIIMathML plugin doesn't want to work for this page...]

I'm reading The Mathematical Mechanic by Mark Levi, a tour of various mathematical results that can be obtained much more directly (albeit without proof) by appealing to our mechanical intuition.

Here's a beautiful proof from the book of Pythagoras' theorem, `a^2 + b^2 = c^2` where `a` and `b` are the opposite and adjacent sides of a right angled triangle and `c` is the hypotenuse.

Consider an ice skater of mass `m` on a perfectly smooth ice rink. The skater starts in the South West corner of the rink and pushes off against the South wall; the skater is now moving North with velocity `a` and hence has kinetic energy `ma^2/2`. Next the skater pushes off against the West wall and adds an Easterly component `b` to their velocity. The energy acquired from this second push is `mb^2/2`. Now, the skater's overall velocity is `c` and overall kinetic energy is `mc^2/2`. But this must be the sum of the energy acquired from each push, hence `ma^2/2 + mb^2/2 = mc^2/2`. Cancel the `m/2` terms and you have Pythagoras' theorem!