HQ9+

Preface FROM THE FUTURE! (2013)

HQ9+ may wind up being my legacy. Created as an elaborate (and admittedly obscure) joke over 12 years ago, it’s somehow developed an enduring popularity that took me completely by surprise.

In hindsight, HQ9+ is the first in a series of minimal, highly-optimized runtime designs that has led to my Forth implementations and my work on Native Client/LLVM.

After a redesign of my website in 2010, the HQ9+ page went away. Three years later I’m still getting several hits per week. So, I’ve recreated it using the exact text from my old website — preserved as a historical artifact except for this preface.

I’d like to give shouts out to the research community for extending HQ9+:

HQ9+ is a tutorial-complete programming language, but I often receive complaints that it is not Turing-complete — typically from people who are not sense-of-humor-complete. To such critics, I offer a variant, HQ9+T; the new T instruction is a programming language combinator that transforms an existing language into a Turing-complete superset. Unfortunately there is not enough room in this preface for my implementation. [Edit: I am not the first to make this joke!]

Original content follows.


The Research

Over winter break of 2000-2001, I ran across Brainf*ck and other extraordinarily esoteric languages. As most students of programming do, I began searching for examples of these languages to help me learn. A great many examples were available, from a great many different authors, but I soon began to notice a pattern: these examples fell neatly into a few categories.

Hello, World!

The quintessential example in any programming language was invented by K&R in their C tutorial. It’s called the Hello World. When a Hello World program is run, it–can you guess?–prints “Hello, world!” to the screen (or teletype or laser-projecting-on-clouds or whatever). This is generally a simple one-line easily-readable statement, which is why it makes for a good first example. (One of the most common exercises for esoteric languages, however, is to make the Hello World as obtuse and and mind-bendingly complex as possible. Take a look at the program that prints “Hi” in Beatnik. The full Hello World would be about twelve times as long.)

99 Bottles of Beer on the Wall

The next set of examples I ran across originated on Usenet many years ago: programs that attempt to print the entire canonical lyrics to “99 Bottles of Beer on the Wall” in the smallest (or craziest) code possible. This is an excellent exercise in recursive, iterative programming, and I knew I had to take it into account.

Quines

The next (particularly cool) set of examples were called “quines.” A quine reproduces its source code to the screen. This is more difficult than it sounds, because very few languages have methods for accessing their own source code. Take this pseudocode example:

print "print "print "print .....

As you can see, to simply print the source code of a program as a literal string (like I tried to do there), your program quickly becomes infinitely long. Thus, quines require the programmer to use all sorts of crazy tricks, making them a wonderful exercise.

The Result

As a result of this research, I created a language called HQ9+. HQ9+ is a very simple language consisting of four operations: H, Q, 9, and +. These operations can be used to create any of the types of example programs described above. They work as follows:

H  Prints "Hello, world!"
Q  Prints the entire text of the source code file.
9  Prints the complete canonical lyrics to "99 Bottles of Beer on the Wall"
+  Increments the accumulator.

HQ9+ is very simple, but allows you to do some things that are very difficult in other languages. For example, here is a program that creates four — count ’em, four — copies of itself on the screen:

qqqq

This produces:

qqqq
qqqq
qqqq
qqqq

Wow! Wasn’t that straightforward?

Interpreters

HQ9+ was well-accepted by the esoteric languages community, and a flurry of interpreters for it, written in every language available, were released. Most of those have been lost to the winds of time (or rather, the winds of my delete key), but I’m working on re-collecting them for distribution here. Those that I’ve managed to find are listed below.