Saturday 26 March 2011

Vim Indentation-based Text Objects

I've created a small Vim plugin for treating indented blocks of code as text objects.

http://www.vim.org/scripts/script.php?script_id=3037

From the documentation:

Vim text objects provide a convenient way to select and operate on various types of objects. These objects include regions surrounded by various types of brackets and various parts of language (ie sentences, paragraphs, etc).

This plugin defines a new text object, based on indentation levels. This is very useful in languages such as Python, in which the syntax defines scope in terms of indentation. Using the objects defined in this plugin, an entire if structure can be quickly selected, for example.

buildtree.mak - An ideal makefile in ~200 lines.

Download link: https://github.com/michaeljsmith/buildtree.mak/blob/master/buildtree.mak

Build systems for C/C++ projects tend to be complex and inscrutable. However often the requirements for a build system are as simple as "compile every source file in this directory tree and link them into a module". Also it would be nice not to require some fancy build system - GNU make is installed pretty much everywhere, right? Surely it's powerful enough to perform this simple task?

Indeed it is. However, nearly all makefiles I've encountered (perhaps I've missed the good ones?) have one or more of the following weaknesses:

  • Dependencies are not automatically tracked, or are done so in a broken way (e.g. if I edit a header file and add a #include, the new dependency is not updated).
  • If dependencies are tracked correctly, updating them performs too much work (e.g. pretty much any makefile that uses cc -M, since if you touch one header file it has to parse every header included by every source file that included the touched header, rather than just that one header!)
  • Object files are built in the same directory as the source files. This is bad because if I want to change a compile setting, I need to delete them all (it's better to have different configurations in different directories, so when I switch I can still just update incrementally).
  • Adding a new source file means manually adding it to the makefile.
  • Requires a huge suite of perl scripts, etc., to implement this functionality, which is pretty much broken anyway, and no-one wants to fix it because the whole thing is so uuurgh...
  • Rely excessively on recursive invocations of make (for why this is bad see http://miller.emu.id.au/pmiller/books/rmch/).
  • Object files get dumped into a single directory - this leads to problems if two source files in different directories have the same name.
  • Dependency tracking is some large, opaque process, which means that make -j is unable to optimize it.

To address this, I've created a simple makefile which I've called buildtree.mak. It does just that - let you build all the source files in a source tree into a module. All generated files are output into a specified directory. This directory mirrors the structure of the source tree. Dependencies are tracked automatically, but updated using as little work as possible.

The minimal dependency tracking utilizes the feature of GNU make where included makefiles are first updated using standard make rules. This means that dependency updating is thoroughly understood by make itself, and therefore it works well with make -j.

It is intended as a template - you can just dump it in your project and rename it to Makefile and hack away at it until it suits your needs. I've released it into the Public Domain, so do what you like with it (acknowledgements are always welcome).

Feedback, bugs and especially patches are welcome.

Thursday 24 March 2011

Morality

This essay was written partly in response to my friend Bryn's article on atheistic ethics, but it is basically just a summary of my views on morality, which are quite simple. I present them here in dialogue form.

Is there an absolute morality?

No. Christians are right when they say that there can be no absolute morality without God. Morality is in no way implied by intelligence.

Then what makes an action right or wrong?

Nothing, there's not really any such concept as right or wrong. People do, however, have a sense of altruism, and because of that they find certain actions morally laudable or repugnant.

So morality just boils down to personal preference.

Put baldly, yes, although I feel that the word 'preference' understates the depth of feeling these moral intuitions involve.

This is appalling - anyone can do whatever they want, and there's nothing I can do to stop them!

There's plenty you can do to stop them, ranging from political activism to direct physical intervention.

I believe this is the nub of the issue - people claim that there is an absolute morality, but what they really mean is that their own personal views are superior to those of everyone else, giving them the right to impose their will on others. These people should stop claiming a divine mandate and admit that it's their own moral intuitions they are following.

The fact is that you have exactly the same options for stopping an action whether it's objectively wrong or not, so facing the facts here will not cause you any extra problems.

But God gave me these moral intuitions! By following them I am doing his will.

If God gave them to you, it's strange that he gave different ones to me, and to some people seemingly none at all. In fact it was evolution that gave them to you.

Rubbish, evolution rewards selfishness, it doesn't explain altruism.

This statement shows a typical misunderstanding of evolution. Evolution rewards selfishness on the part of the gene, not on the part of the individual. In fact since we share genes, it rewards altruism on the part of the individual, at least in certain situations.

Well I'm glad I don't live in a society full of people like you. Luckily most people accept that some things are right and wrong - otherwise there would be chaos!

Let's look at this problem the other way around - what do we hope to gain by establishing an idea of absolute right and wrong? Presumably to get everyone to agree on correct behaviour. However, since no-one can agree on what exactly is the absolutely right course of action, agreeing that such a thing exists gets us nowhere. In fact it makes matters worse, since now instead of simply arguing whether a particular action is right we instead debate systems of ethics on an ever more abstract level. Ethicists write volumes discussing the philosophical principles that approximate our personal intuition on the assumption that by doing so they are getting closer to a hypothetical ideal, when instead they could simply abandon the pursuit and accept that those intuitions are themselves the ultimate goal.

And why should we belittle our humanity as the source of our values? Why should we look to an external supernatural force to tell us what to do? We are the ones living our lives. We are the ones who cause suffering to others through selfishness, or happiness through altruism, and are affected ourselves in turn. If morality can be found anywhere, it must be from within,