blarg?

academia

I gave this talk at FSOSS last week, in which I try to reclaim the term “Social Engineering”, so that it stops meaning “get the receptionist to give you their password” and starts meaning “Measuring community growth and turning that into processes and practices that work.”

I thought it went well, though listening to it I can see I’ve got a couple of verbal tics to work on. Gotta stop using ‘um’ and ‘right’ as punctuation.

I may revisit this later. Consider this a late draft. I’m calling this done.

“Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.” — Stan Kelly-Bootle

Sometimes somebody says something to me, like a whisper of a hint of an echo of something half-forgotten, and it lands on me like an invocation. The mania sets in, and it isn’t enough to believe; I have to know.

I’ve spent far more effort than is sensible this month crawling down a rabbit hole disguised, as they often are, as a straightforward question: why do programmers start counting at zero?

Now: stop right there. By now your peripheral vision should have convinced you that this is a long article, and I’m not here to waste your time. But if you’re gearing up to tell me about efficient pointer arithmetic or binary addition or something, you’re wrong. You don’t think you’re wrong and that’s part of a much larger problem, but you’re still wrong.

For some backstory, on the off chance anyone still reading by this paragraph isn’t an IT professional of some stripe: most computer languages including C/C++, Perl, Python, some (but not all!) versions of Lisp, many others – are “zero-origin” or “zero-indexed”. That is to say, in an array A with 8 elements in it, the first element is A[0], and the last is A[7]. This isn’t universally true, though, and other languages from the same (and earlier!) eras are sometimes one-indexed, going from A[1] to A[8].

While it’s a relatively rare practice in modern languages, one-origin arrays certainly aren’t dead; there’s a lot of blood pumping through Lua these days, not to mention MATLAB, Mathematica and a handful of others. If you’re feeling particularly adventurous Haskell apparently lets you pick your poison at startup, and in what has to be the most lunatic thing I’ve seen on a piece of silicon since I found out the MIPS architecture had runtime-mutable endianness, Visual Basic (up to v6.0) featured the OPTION BASE flag, letting you flip that coin on a per-module basis. Zero- and one-origin arrays in different corners of the same program! It’s just software, why not?

All that is to say that starting at 1 is not an unreasonable position at all; to a typical human thinking about the zeroth element of an array doesn’t make any more sense than trying to catch the zeroth bus that comes by, but we’ve clearly ended up here somehow. So what’s the story there?

The usual arguments involving pointer arithmetic and incrementing by sizeof(struct) and so forth describe features that are nice enough once you’ve got the hang of them, but they’re also post-facto justifications. This is obvious if you take the most cursory look at the history of programming languages; C inherited its array semantics from B, which inherited them in turn from BCPL, and though BCPL arrays are zero-origin, the language doesn’t support pointer arithmetic, much less data structures. On top of that other languages that antedate BCPL and C aren’t zero-indexed. Algol 60 uses one-indexed arrays, and arrays in Fortran are arbitrarily indexed – they’re just a range from X to Y, and X and Y don’t even need to be positive integers.

So by the early 1960’s, there are three different approaches to the data structure we now call an array.

  • Zero-indexed, in which the array index carries no particular semantics beyond its implementation in machine code.
  • One-indexed, identical to the matrix notation people have been using for quite some time. It comes at the cost of a CPU instruction to manage the offset; usability isn’t free.
  • Arbitrary indices, in which the range is significant with regards to the problem you’re up against.

So if your answer started with “because in C…”, you’ve been repeating a good story you heard one time, without ever asking yourself if it’s true. It’s not about *i = a + n*sizeof(x) because pointers and structs didn’t exist. And that’s the most coherent argument I can find; there are dozens of other arguments for zero-indexing involving “natural numbers” or “elegance” or some other unresearched hippie voodoo nonsense that are either wrong or too dumb to rise to the level of wrong.

The fact of it is this: before pointers, structs, C and Unix existed, at a time when other languages with a lot of resources and (by the standard of the day) user populations behind them were one- or arbitrarily-indexed, somebody decided that the right thing was for arrays to start at zero.

So I found that person and asked him.

His name is Dr. Martin Richards; he’s the creator of BCPL, now almost 7 years into retirement; you’ve probably heard of one of his doctoral students Eben Upton, creator of the Raspberry Pi. I emailed him to ask why he decided to start counting arrays from zero, way back then. He replied that…

As for BCPL and C subscripts starting at zero. BCPL was essentially designed as typeless language close to machine code. Just as in machine code registers are typically all the same size and contain values that represent almost anything, such as integers, machine addresses, truth values, characters, etc. BCPL has typeless variables just like machine registers capable of representing anything. If a BCPL variable represents a pointer, it points to one or more consecutive words of memory. These words are the same size as BCPL variables. Just as machine code allows address arithmetic so does BCPL, so if p is a pointer p+1 is a pointer to the next word after the one p points to. Naturally p+0 has the same value as p. The monodic indirection operator ! takes a pointer as it’s argument and returns the contents of the word pointed to. If v is a pointer !(v+I) will access the word pointed to by v+I. As I varies from zero upwards we access consecutive locations starting at the one pointed to by v when I is zero. The dyadic version of ! is defined so that v!i = !(v+I). v!i behaves like a subscripted expression with v being a one dimensional array and I being an integer subscript. It is entirely natural for the first element of the array to have subscript zero. C copied BCPL’s approach using * for monodic ! and [ ] for array subscription. Note that, in BCPL v!5 = !(v+5) = !(5+v) = 5!v. The same happens in C, v[5] = 5[v]. I can see no sensible reason why the first element of a BCPL array should have subscript one. Note that 5!v is rather like a field selector accessing a field in a structure pointed to by v.

This is interesting for a number of reasons, though I’ll leave their enumeration to your discretion. The one that I find most striking, though, is that this is the earliest example I can find of the understanding that a programming language is a user interface, and that there are difficult, subtle tradeoffs to make between resources and usability. Remember, all this was at a time when everything about the future of human-computer interaction was up in the air, from the shape of the keyboard and the glyphs on the switches and keycaps right down to how the ones and zeros were manifested in paper ribbon and bare metal; this note by the late Dennis Ritchie might give you a taste of the situation, where he mentions that five years later one of the primary reasons they went with C’s square-bracket array notation was that it was getting steadily easier to reliably find square brackets on the world’s keyboards.

“Now just a second, Hoye”, I can hear you muttering. “I’ve looked at the BCPL manual and read Dr. Richards’ explanation and you’re not fooling anyone. That looks a lot like the efficient-pointer-arithmetic argument you were frothing about, except with exclamation points.” And you’d be very close to right. That’s exactly what it is – the distinction is where those efficiencies take place, and why.

BCPL was first compiled on an IBM 7094here’s a picture of the console, though the entire computer took up a large room – running CTSS – the Compatible Time Sharing System – that antedates Unix much as BCPL antedates C. There’s no malloc() in that context, because there’s nobody to share the memory core with. You get the entire machine and the clock starts ticking, and when your wall-clock time block runs out that’s it. But here’s the thing: in that context none of the offset-calculations we’re supposedly economizing are calculated at execution time. All that work is done ahead of time by the compiler.

You read that right. That sheet-metal, “wibble-wibble-wibble” noise your brain is making is exactly the right reaction.

Whatever justifications or advantages came along later – and it’s true, you do save a few processor cycles here and there and that’s nice – the reason we started using zero-indexed arrays was because it shaved a couple of processor cycles off of a program’s compilation time. Not execution time; compile time.

Does it get better? Oh, it gets better:

IBM had been very generous to MIT in the fifties and sixties, donating or discounting its biggest scientific computers. When a new top of the line 36-bit scientific machine came out, MIT expected to get one. In the early sixties, the deal was that MIT got one 8-hour shift, all the other New England colleges and universities got a shift, and the third shift was available to IBM for its own use. One use IBM made of its share was yacht handicapping: the President of IBM raced big yachts on Long Island Sound, and these boats were assigned handicap points by a complicated formula. There was a special job deck kept at the MIT Computation Center, and if a request came in to run it, operators were to stop whatever was running on the machine and do the yacht handicapping job immediately.

Jobs on the IBM 7090, one generation behind the 7094, were batch-processed, not timeshared; you queued up your job along with a wall-clock estimate of how long it would take, and if it didn’t finish it was pulled off the machine, the next job in the queue went in and you got to try again whenever your next block of allocated time happened to be. As in any economy, there is a social context as well as a technical context, and it isn’t just about managing cost, it’s also about managing risk. A programmer isn’t just racing the clock, they’re also racing the possibility that somebody will come along and bump their job and everyone else’s out of the queue.

I asked Tom Van Vleck, author of the above paragraph and also now retired, how that worked. He replied in part that on the 7090…

“User jobs were submitted on cards to the system operator, stacked up in a big tray, and a rudimentary system read, loaded, and ran jobs in sequence. Typical batch systems had accounting systems that read an ID card at the beginning of a user deck and punched a usage card at end of job. User jobs usually specified a time estimate on the ID card, and would be terminated if they ran over. Users who ran too many jobs or too long would use up their allocated time. A user could arrange for a long computation to checkpoint its state and storage to tape, and to subsequently restore the checkpoint and start up again.

The yacht handicapping job pertained to batch processing on the MIT 7090 at MIT. It was rare — a few times a year.”

So: the technical reason we started counting arrays at zero is that in the mid-1960’s, you could shave a few cycles off of a program’s compilation time on an IBM 7094. The social reason is that we had to save every cycle we could, because if the job didn’t finish fast it might not finish at all and you never know when you’re getting bumped off the hardware because the President of IBM just called and fuck your thesis, it’s yacht-racing time.

There are a few points I want to make here.

The first thing is that as far as I can tell nobody has ever actually looked this up.

Whatever programmers think about themselves and these towering logic-engines we’ve erected, we’re a lot more superstitious than we realize. We tell and retell this collection of unsourced, inaccurate stories about the nature of the world without ever doing the research ourselves, and there’s no other word for that but “mythology”. Worse, by obscuring the technical and social conditions that led humans to make these technical and social decisions, by talking about the nature of computing as we find it today as though it’s an inevitable consequence of an immutable set of physical laws, we’re effectively denying any responsibility for how we got here. And worse than that, by refusing to dig into our history and understand the social and technical motivations for those choices, by steadfastly refusing to investigate the difference between a motive and a justification, we’re disavowing any agency we might have over the shape of the future. We just keep mouthing platitudes and pretending the way things are is nobody’s fault, and the more history you learn and the more you look at the sad state of modern computing the the more pathetic and irresponsible that sounds.

Part of the problem is access to the historical record, of course. I was in favor of Open Access publication before, but writing this up has cemented it: if you’re on the outside edge of academia, $20/paper for any research that doesn’t have a business case and a deep-pocketed backer is completely untenable, and speculative or historic research that might require reading dozens of papers to shed some light on longstanding questions is basically impossible. There might have been a time when this was OK and everyone who had access to or cared about computers was already an IEEE/ACM member, but right now the IEEE – both as a knowledge repository and a social network – is a single point of a lot of silent failure. “$20 for a forty-year-old research paper” is functionally indistinguishable from “gone”, and I’m reduced to emailing retirees to ask them what they remember from a lifetime ago because I can’t afford to read the source material.

The second thing is how profoundly resistant to change or growth this field is, and apparently has always been. If you haven’t seen Bret Victor’s talk about The Future Of Programming as seen from 1975 you should, because it’s exactly on point. Over and over again as I’ve dredged through this stuff, I kept finding programming constructs, ideas and approaches we call part of “modern” programming if we attempt them at all, sitting abandoned in 45-year-old demo code for dead languages. And to be clear: that was always a choice. Over and over again tools meant to make it easier for humans to approach big problems are discarded in favor of tools that are easier to teach to computers, and that decision is described as an inevitability.

This isn’t just Worse Is Better, this is “Worse Is All You Get Forever”. How many off-by-one disasters could we have avoided if the “foreach” construct that existed in BCPL had made it into C? How much more insight would all of us have into our code if we’d put the time into making Michael Chastain’s nearly-omniscient debugging framework – PTRACE_SINGLESTEP_BACKWARDS! – work in 1995? When I found this article by John Backus wondering if we can get away from Von Neumann architecture completely, I wonder where that ambition to rethink our underpinnings went. But the fact of it is that it didn’t go anywhere. Changing how you think is hard and the payoff is uncertain, so by and large we decided not to. Nobody wanted to learn how to play, much less build, Engelbart’s Violin, and instead everyone gets a box of broken kazoos.

In truth maybe somebody tried – maybe even succeeded! – but it would cost me hundreds of dollars to even start looking for an informed guess, so that’s the end of that.

It’s hard for me to believe that the IEEE’s membership isn’t going off a demographic cliff these days as their membership ages, and it must be awful knowing they’ve got decades of delicious, piping-hot research cooked up that nobody is ordering while the world’s coders are lining up to slurp watery gruel out of a Stack-Overflow-shaped trough and pretend they’re well-fed. You might not be surprised to hear that I’ve got a proposal to address both those problems; I’ll let you work out what it might be.

Today I learned that “I did your mom” jokes are the summit of cultured humour.

DEMETRIUS: Villain, what hast thou done?
AARON: That which thou canst not undo.
CHIRON: Thou hast undone our mother.
AARON: Villain, I have done thy mother.

That’s from Shakespeare’s Titus Andronicus, of all things.

I miss Nick; he would have looked at me like I was dumb for not knowing this.

I’ve decided that if I find out a job applicant has internet-bragged that they could implement some major (Kickstarter, Ebay, Etsy, Facebook, anything…) website’s functionality in a week with Ruby – and it’s always Ruby, lately – I’m going to give them an interview right away, just so I can ask them why they haven’t.

I’d never give them a job. I just want to watch them squirm when I ask the question.

I made this presentation to Seneca’s Free Software and Open Source Symposium last year; it is dreadfully embarassing, revealing mainly that I’m a terrible speaker who tells weak jokes, goes off into the weeds too often, rambles and says “um” far and away too much. This is just the voice track over my slides, which I’ll put up later this evening.

I’m sure the general outline of the presentation I’d like to have given is in there somewhere, but here you go.

Dark Helmet: Who is he?
Colonel Sandurz: He’s an Asshole, sir.
Dark Helmet: I know that! What’s his name?
Colonel Sandurz: That is his name, sir. Asshole. Major Asshole.

Have I mentioned that the No Asshole Rule has profound geopolitical implications? It’s an idea that’s been rattling around my head for a while.

JWZ links to the question:

In Egypt earlier this year, the cops refused to attack the people. East Germany and then the whole Iron Curtain collapsed when the local cops wouldn’t smash heads when Erich Honecker ordered it. What about America? Where are the cops who walked off the job rather than attack their neighbors drowning in debt and despair?

Daniel Davies provides the answer:

And so that brings me to a useful piece of advice for any readers who are aspiring dictators, one that the Communists knew, Suharto knew, but that some modern day tyrants seem to have forgotten. There is always a level of civil unrest that outstrips the capability of even the most loyal and largest regular armed forces to deal with. In all likelihood, as a medium sized emerging market, you will have a capital city with a population of about five or six million, meaning potentially as many as three million adults on the streets in the worst case. Your total active-duty armed forces are unlikely to be a tenth of that. When it becomes a numbers game, there is only one thing that can save you.

And that is, a reactionary citizens’ militia, to combat the revolutionary citizens’ militia. Former socialist republics always used to be fond of buses full of coal miners from way out the back of beyond, but the Iranian basijis are the same sort of thing. Basically, what you need is a large population who are a few rungs up from the bottom of society, who aren’t interested in freedom and who hate young people. In other words, arseholes. Arseholes, considered as a strategic entity, have the one useful characteristic that is the only useful characteristic in the context of an Egyptian-style popular uprising – there are f—ing millions of them.

This is my advice to any aspiring dictator; early on in your career, identify and inventory all the self-pitying, bullying shitheads your country has to offer. Anyone with a grievance, a beer belly and enough strength to swing a pickaxe handle will do. You don’t need to bother with military training or discipline because they’re hopefully never going to be used as a proper military force – just concentrate on nurturing their sense that they, despite appearances, are the backbone of the country, and allowing them to understand that although rules are rules, there are some people who just need a slap. The bigger and burlier the better, but when the time comes they’ll be fighting in groups against people weaker than themselves, often under cover of darkness, so numbers are more important than anything else. The extractive industries are indeed often a good source, as are demobbed veterans (Zimbabwe) or the laity of an established religion.

I think this is my new rule for assessing the stability of any dictatorship around the world, and I am on the lookout for any Francis Fukuyama-style book contracts. The key factor in determining the survival of repressive regimes isn’t economics, religion or military success. It’s arseholes.

If you’ve been reading the news lately, this may sound familiar. It’s that sentence near the end there – “The bigger and burlier the better, but when the time comes they’ll be fighting in groups against people weaker than themselves, often under cover of darkness, so numbers are more important than anything else” – that makes it so horribly prescient.

You Shouldn't Be Rapping

My little startup is moving along quite nicely, have I mentioned that? 2010 was a good year, and 2011 is looking extremely promising, in large part because of the awesome people I’m working with now and looking to hire. But I’d like to clear up one little thing about that; noted Seneca professor Dave Humphrey has recently observed about hiring college students:

But there’s at least one big problem, and it is perfectly reflected in a mail I got recently from a mid-sized tech company:

“…we normally only hire from universities, but might be open to a college student.”

This was followed by an invitation for me to come sing and dance for them, in order to prove our students were worth considering. I’ve done a lot of singing and dancing over the past five years and I’m starting to tire of the intellectual snobbery and education elitism that claims to want one thing, but interviews and hires for something else. Meanwhile, we’re shipping software. Meanwhile, we’re getting real shit done.

Incomprehensible. He even has the audacity to go on to argue that this is somehow a problem! And just I want to make it clear to businesses that operating in those niches in or near my own: this is not a problem at all for you. You should absolutely keep doing exactly what you’re doing. If you could just forget about college students outright, circular-file their resumes and ignore their emails, that would be great.

Trust me, future competitors, just keep doing that. I really appreciate it.

Pleasingly Apocalyptic

This is interesting, and stirs some pleasingly cyberpunkish ideas around in my brain. Three months ago, from The Atlantic:

Mysterious and possibly nefarious trading algorithms are operating every minute of every day in the nation’s stock exchanges.

What they do doesn’t show up in Google Finance, let alone in the pages of the Wall Street Journal. No one really knows how they operate or why. But over the past few weeks, Nanex, a data services firm has dragged some of the odder algorithm specimens into the light.

The trading bots visualized in the stock charts in this story aren’t doing anything that could be construed to help the market. Unknown entities for unknown reasons are sending thousands of orders a second through the electronic stock exchanges with no intent to actually trade. Often, the buy or sell prices that they are offering are so far from the market price that there’s no way they’d ever be part of a trade. The bots sketch out odd patterns with their orders, leaving patterns in the data that are largely invisible to market participants.

This week, from various sources including Futures Magazine:

“Two Norwegian traders, Svend Egil Larsen and Peder Veiby, were handed suspended prison sentences on charges related to market manipulation. According to the Financial Times, the two were charged for figuring out how a computerized trading system at a large American firm that is a subsidy of Interactive Brokers would react to certain stock moves and using that information to manipulate the price of low-volume stocks.”

From the The New York Times:

“But Mr. Brosveet says the court would never have ruled the way it did “if it was just a stupid human being” on the other side of the trade. Instead, it was a computer, and “the computer must be held as a responsible actor,” he said.”

The examples of the patterns mentioned in the Atlantic article are fascinating, and are almost certainly exactly doing procedurally what these clever Norwegians were doing (apparently) manually. Except thousands of times a second, looking for a response that’s not clear; I wonder how many of the stock market’s algorithm designers even have threat models, much less models that account for subtly malicious input. I suspect all of them will, by this time next week!

Exhibit 1: From “A Close Shave“, the Knit-O-Matic.

Exhibit 2: A Sheep-Shearing Australian Robot. From the description:

This robot clipped around 400 fleeces between 1985 and 1993 at the University of Western Australia. The sheep was in its natural state, with no drugs or artificial means of keeping it still. In over 1000 tests with this robot, only a few sheep were seriously injured, and there were many times fewer skin cuts than human shearers inflict because the robot could react in thousandths of a second.

No artificial means of keeping it still besides being held firm in the iron clutch of an automated shearing robot, he manifestly failed to add.

Crystalline

December 23rd:

Hi, my name’s Mike Hoye, and I worked until recently for TVOntario.

I understand, that as per http://www-cs-faculty.stanford.edu/~knuth/help.html you are looking for one Janet K. Webb, and with a bit of effort I’ve managed to track her down.

Her current mailing address is:

Janet K. Webb
—-redacted—-
—-redacted—-

I understand that Mr. Knuth doesn’t receive these messages directly, but if you could pass on my thanks for his work I would be grateful.

Take care, and happy holidays.


Mike Hoye

Today:

Dear Mike, Many thanks for sending Janet Webb’s address. (Her name
wasn’t on my page “help.html” but rather on “address.html“, since
I wanted to reward her for reporting an error in The Art of Computer
Programming. Now I’ll be able to put her name on my page “boss.html“.)

Best wishes for 2009! — Don Knuth

I feel like a twelve-year-old girl at a boy-band concert. I can’t decide whether to scream or pass out.

I believe I am now going to buy one of these shirts.