Hash tables in I7

What’s the best equivalent of a hash table in Inform 7? In this particular instance, I just want a hash from strings to integers. The most idiomatic setup seems to be an I7 table, but I’m not sure whether it is implemented as a hash table under the hood (and this would simply be a two-column table), and runtime speed is relevant here. The alternative is just to write one myself in I6 (strings are easy to hash), but I don’t want to reinvent the wheel, especially when hash functions are pretty basic.

Also, is there an abbreviation for the syntax for accessing tables? There’s no justification for writing out something like ‘the value corresponding to a key of “foo” in the Table of Bar’ whenever I want to grab a value from it. Of course I can create a wrapper around it for syntactic sugar, but is there really no equivalent of something as simple “f[key] = 1” in I7?

There’s no hash table/map in Inform 7. Tables are just two dimensional arrays.

My Data Structures extension (for 6M62 only) does include a map kind, but it is not a hashmap. Its performance is not good if you use lots of map entries, but it’s still basically a prototype. I’ve proposed incorporating it into the core of Inform in the future, in which case we’ll use a hashmap or btreemap or something.

Inform tends to stick to natural language phrases, even if very wordy. Authors are free to make their own shortcut phrases as they like.

1 Like

Guess I’ll drop in some I6 code, then. I can certainly write my own phrases, but I don’t want to use words; I want to use nice, compact symbols. Oh well.

Hashing I7 strings is never going to be speedy. If the string is stored compressed, you have to render it out as a sequence of characters (TEXT_TY_Temporarily_Transmute()) before you can hash it.

Also, strings with substitutions could change their substituted form (and thus their hash key) without warning.

Does it make sense to think in terms of an enumerated value (kind of value) and only worry about strings at print time?

2 Likes

I love Inform 7 but this verbosity in the case of dealing with data structures and variables has always been tough. I do wonder if it would be possible to add syntactic sugar (or make it possible to add syntactic sugar) for some aspects of the language in the future?

1 Like

I say this with all due respect: Inform 7 might not be the right language for you then. The verbosity is part of the design and you’ll find it in every aspect of the language and library.

I7 is absolutely not the right language for me; it’s just about put me off writing interactive fiction at this point. I hate it, but what’s the alternative? IF already has a very small audience, and writing for a niche subset of that audience might as well be talking to myself— not to mention the problems with finding co-authors, answers for technical questions, libraries to work with, and so on.

TADS 3? Dialog? There’s active discussion on those systems, at least.

3 Likes

I would love a version of I7 for programmers, or at least one without the language’s bloody-minded insistence on verbose, inconsistent names for everything. There’s so much to like about I7— the rules-based setup, the IDE, the extensive debugging tools— but writing a game in it is like a massive guess-the-verb problem.

Active, but not very popular. It’s fine. Like everyone else here, I’ve got a pile of writing and coding hobby projects on the back burner anyway.

As for the hashing idea: These would be constant strings, and I could probably get away with replacing them with a list of enumerated values. The only catch— and it’s a minor one— is that the hash table wouldn’t have a fixed size.

You might be interested in @Zed’s Code extension: extensions/Zed Lopez/Code.i7x at 9.3 · i7/extensions · GitHub

2 Likes

Thanks, that looks promising.

May I ask what it is that you’re trying to accomplish (i.e. what it is that you want to do with the hash table)?

1 Like

Well, the “finding an audience” problem doesn’t depend on your choice of system so much; Inform 7, Inform 6 (a totally different language), TADS, and Dialog can all output a web page, and a web browser is the world’s most universal interpreter nowadays.

2 Likes

Also, for some more specific advice:

  • Inform 6 is the predecessor to I7 and is a totally different language. (I7 actually compiles down to I6.) It’s a very low-level, close-to-the-metal language that outright expects you to insert your own assembly instructions when you need to do something fancy that the standard library doesn’t support. There are alternate libraries for it that make incredibly small compiled binaries, to run on retro hardware directly. I’d say its analogue in traditional programming languages is C.
  • TADS is a higher-level language with an incredibly extensive and elaborate library—multiple separate libraries, in fact, with Adv3 and Adv3Lite both seeing use. This is the only one on the list I’ve never used, but it’s much beloved by those who do. This is also the only one of these I’d expect to support hash tables right out of the box; when I say its standard library is extensive, I mean it! To continue the metaphor, I’d say it’s the Java analogue.
  • Dialog is a beautifully simple and elegant language based on Prolog. And like Prolog, it expects a totally different style of programming, mostly declarative rather than imperative. I find learning and using it to be a very fun brainteaser. The standard library is lightweight and built to be easily modified. It’s the LISP of IF domain-specific languages.

So the question is, are you the sort of person who prefers C, Java, or LISP? You’ll find active communities for all three of them on the forum.

3 Likes

You can make associative arrays (what Perl or Ruby people call hashes) with relations, but there are a bunch of bugs with relations and texts in 9.3/6M62. 10.1 is better, current Inform under development is better still. They won’t be fast, though, and a lot of things you might want to do that should be trivial, like writing a function that builds and returns a hash of texts to lists of objects, say, would end up infeasibly slow if it were dealing with a lot of data.

I cannot recommend Code for production use!

3 Likes

Ability to use hash tables is in fact one of the primary reasons I chose TADS3 for my current project. One of the first technical tasks I wanted to be sure I could implement in an IF framework was a poker hand evaluator using lookups. That was easy-ish to do in TADS3, and I couldn’t figure out how to do it at all in I7.

Performance for most lookups is fine. Main caveats I’d add about TADS3, in terms of general programming performance, are:

  • no native implementation of unsigned integers
  • floating point, if you need it, is done in the VM and is therefore slow

That said, it “feels” much more like a general-purpose programming language with some domain-specific sugar, as opposed to the “natural(-ish) language for programming” thing I7 has going on. Although I’m by no means an expert in I7.

3 Likes

As someone who doesn’t have the toolchain to compile the latest Inform 10.2 binaries would it be possible for somebody to add a binaries release to the GitHub repo? I’m very excited about the upcoming features but I always make a mess of it whenever I have tried to compile binaries (for Windows, Linux or Mac, all of which I use).

I gotta say I’m not sure this is completely fair to the language or for the many “real” programmers using it. There are some incredibly advanced things this language is capable of—check out the contents of WI-13 (Relations) and WI-22 (Advanced Phrases) for example.

Another thought is that often in a new language the tendency is to look for a specific feature we think we need when maybe we never needed that feature at all, or the language provided better, more elegant solutions. I definitely did this moving from Apple Basic to Think Pascal to Python to Common Lisp to Inform 7…There are definitely still some syntax I’d love to be able to use, but I still think it’s a brilliant language with some really beautiful ideas.

What is the specific problems/use case you’re trying to solve?

3 Likes

I’d say it’s a brilliant idea for a language with some beautiful ideas and terrible execution. I vehemently disagree with the idea that the code of an interactive fiction should look like its prose (for reason that aren’t worth ranting about here), and having the language go through such extreme measure to obfuscate what should be straightforward things (functional calls, data structures, assignments, etc.) is intensely frustrating. It’s an unnecessary pain point with no benefit.

Also, I’m not saying I7 is not a “real” language or not for “real” programmers. It does have an intense fear of symbols or standard programming conventions, which I assume is a deliberate design choice to make the system less intimidating to newcomers, but that’s hardly disqualifying it from being a “real” language. (I’d also disagree with the idea that it winds up being less intimidating, since playing guess-the-verb with the compiler becomes very tedious very quickly.)

With all due (and considerable!) respect, I’m not looking for a solution to the specific programming problem I’m dealing with. I’ve written a couple games in I6 and one in I7 already, and I’m fine coding. If hash tables aren’t available in I7, there are other data structures that I can solve the problem with.

1 Like