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.
- Someone wrote a Wikipedia article sometime in the mid 2000s, which has apparently been the subject of an ongoing revert/delete war. Its counterpart on the Simple English Wikipedia has survived; since HQ9+ is itself a simple language, this seems fitting.
- It’s the topic of at least one Stack Overflow question.
- I still get interpreter implementations in my inbox, which unfortunately I never have time to post here.
- Several colleagues at Google, mostly from the programming languages community, have asked if I’m “that” Cliff L. Biffle — “the one who wrote HQ9+?” (Wrote is too strong a word, since I never released an implementation.)
- This may be apocryphal, but someone once told me they heard HQ9+ mentioned in a talk by Guy Steele, who is an actual programming language designer. If true, this is hilarious.
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+:
- David Morgan-Mar’s HQ9++ adds object-oriented features, which one needs to complete any language tutorial since the late 90s.
- Melikamp’s HQ9+- adds support for modern error handling paradigms, including asynchronous exceptions, user-defined exception types, and syntax checking.
- Kenner Gordon’s HQ9+2D extends the instruction space to two dimensions, which is an improvement for writing complex programs, but seems like it would scale poorly compared to a proper four dimensional language.
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.
- HQ9+ in Ocaml courtesy of Markus Kliegl