What is the 'accumulator' in HQ9+?
Asked Answered
A

7

17

I was just reading a bit about the HQ9+ programming language:

and it tells me something about a so-called “accumulator” which can be incremented, but not accessed. Also, using + doesn't manipulate the result, so that the input

H+H

gives the result:

Hello World
Hello World

Can anyone explain me how this works, what it does, and whether it makes any sense? Thanks.

Ashcroft answered 4/5, 2010 at 14:59 Comment(0)
S
14

Having written an implementation, I think I can say without a doubt that it makes no sense at all. I advise you to not worry about it; it's a very silly language after all.

Silvester answered 4/5, 2010 at 15:4 Comment(2)
@Konig - based on the description of the language it seems rather useless and you can probably do whatever you want with the + as long as it doesn't affect anything else. I would just ignore it in the implementation since incrementing something you can't access makes no sense.Construction
Wouldn't it technically be non-standard-compliant if it doesn't actually implement a variable somewhere? (Better mark it as volatile or the equivalent if necessary, so the optimizer doesn't break it.)Spear
C
17

Having recently completed an implementation in Clojure (which follows) I can safely say that the accumulator is absolutely central to a successful implementation of HQ9+. Without it one would be left with an implementation of HQ9 which, while doubtless worthy in and of itself, is clearly different, and thus HQ9+ without an accumulator, and the instruction to increment it, would thus NOT be an implementation of HQ9+.

(Editor's note: Bob has taken his meds today but they haven't quite kicked in yet; thus, further explanation is perhaps needed. What I believe Bob is trying to say is that HQ9+ is useless as a programming language, per se; however, implementing it can actually be useful in the context of learning how to implement something successfully in a new language. OK, I'll just go and curl up quietly in the back of Bob's brain now and let him get back to doing...whatever it is he does when I'm not minding the store...).

Anyways...implementation in Clojure follows:

(defn hq9+ [& args]
  "HQ9+ interpreter"

  (loop [program      (apply concat args)
         accumulator  0]
    (if (not (empty? program))
      (case (first program)
        \H (println "Hello, World!")
        \Q (println (first (concat args)))
        \9 (apply println (map #(str % " bottles of beer on the wall, "
                                      % " bottles of beer, if one of those bottles should happen to fall, "
                                      (if (> % 0) (- % 1) 99) " bottles of beer on the wall") (reverse (range 100))))
        \+ (inc accumulator)
            (println "invalid instruction: " (first program)))) ; default case
    (if (> (count program) 1)
       (recur (rest program) accumulator))))

Note that this implementation only accepts commands passed into the function as parameters; it doesn't read a file for its program. This may be remedied in future releases. Also note that this is a "strict" implementation of the language - the original page (at the Wayback Machine) clearly shows that only UPPER CASE 'H's and 'Q's should be accepted, although it implies that lower-case letters may also be accepted. Since part of the point of implementing any programming language is to strictly adhere to the specification as written this version of HQ9+ is written to only accept upper-case letters. Should the need arise I am fully prepared to found a religion, tentatively named the CONVOCATION OF THE HOLY CAPS LOCK, which will declare the use of upper-case to be COMMANDED BY FRED (our god - Fred - it seems like such a friendly name for a god, doesn't it?), and will deem the use of lower-case letters to be anathema...I MEAN, TO BE ANATHEMA!

Share and enjoy.

Coffin answered 25/1, 2014 at 17:56 Comment(1)
God may reject lower-case letters, but all of the example code provided in the detailed specification used lower-case letters. Case-insensitivity is the obvious intent. (hq9+ is truly the most lamentably implemented language around. Language designers take note, and supply a standard set of tests with your spec.)Glaciate
S
14

Having written an implementation, I think I can say without a doubt that it makes no sense at all. I advise you to not worry about it; it's a very silly language after all.

Silvester answered 4/5, 2010 at 15:4 Comment(2)
@Konig - based on the description of the language it seems rather useless and you can probably do whatever you want with the + as long as it doesn't affect anything else. I would just ignore it in the implementation since incrementing something you can't access makes no sense.Construction
Wouldn't it technically be non-standard-compliant if it doesn't actually implement a variable somewhere? (Better mark it as volatile or the equivalent if necessary, so the optimizer doesn't break it.)Spear
S
14

It's a joke.

There's also an object-oriented extension of HQ9+, called HQ9++. It has a new command ++ which instantiates an object, and, for reasons of backwards-compatibility, also increments the accumulator register twice. And again, since there is no way to store, retrieve, access, manipulate, print or otherwise affect an object, it's completely useless.

Sanbo answered 4/5, 2010 at 15:39 Comment(0)
B
6

It increments something not accessible, not spec-defined, and apparently not really even used. I'd say you can implement it however you want or possibly not at all.

Bangka answered 4/5, 2010 at 15:16 Comment(3)
Accumulator can be accessed by metaprograms. Therefore it's necessary to follow the specs.Gratiana
How can a metaprogram access the accumulator? A metaprogram might be able to do this for a specific implementation that implements the accumulator in a specific way. However, in the absence of clear language specification that describes what the accumulator does, the behavior of any such metaprogram will not be well defined.Cockleboat
To make this argument you would have to create a new language (perhaps called HQ9+14STD or something like this) with a precise specification of the accumulator's behavior.Cockleboat
C
5

The right answer is one that has been hinted at by the other answers but not quite stated explicitly: the effect of incrementing the accumulator is undefined by the language specification and left as a choice of the implementation.

Cockleboat answered 22/1, 2014 at 18:35 Comment(0)
G
3

Actually, I am mistaken.

The accumulator is the register to which the result of the last calculation is stored. In an Intel x86, any register may be specified as the accumulator, except in the case of MUL.

Source:

http://en.wikipedia.org/wiki/Accumulator_(computing)

I was quite surprised the first time I visited the third site in your question to find out a schoolmate of mine wrote the OCaml implementation at the bottom of the page.

(updated site link)

Gorey answered 5/10, 2010 at 8:57 Comment(0)
F
0

I think that there is, or there must be, a reason for this accumulator and the most important operation on it - increment: future compatibility.

Very often we see that a language is invented, often inspirated by some other language, with of course some salt (new concepts, or at least some improvement). Later, when the language spreads, problems arise and modifications, additions or whatever are introduced. That is the same as saying "we were wrong, this thing was necessary but we didn't tought of it at the time".

Well, this accumulator idea in HQ9+ is exactly the opposite. In the future, when the language will be spread, nobody will be able to say "we need an accumulator, but HQ9+ lacks it", because the standard of the language, even in its first draft, states that an accumulator is present and it is even modifiable (otherwise, it would be a non-sense).

Fairman answered 21/10, 2022 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.