Over a million developers have joined DZone.

Natural Language Programming and Inform 7: An Intro

Inform 7 is known as a programming language that is closer to natural language than any other. This article will intro you to the language and give you a demo of what it can do.

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

I'd really love your thoughts on this article. Please do comment below! 

If you're reading this, there's a pretty good chance that you have written a program/script/class/method/whatever involving the Fibonacci sequence. It's a great way to logic through a new language (after you've Hello Worlded, right?), and often it's a good intro into recursion. So this code likely won't be too unfamiliar to you:

public class fib {

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        System.out.println("Enter a number: ");
        int num = scan.nextInt();

    private static int calculate(int n){
        if (n <= 1)
            return 1;
            return calculate(n - 1) + calculate(n - 2);

We all know that Java is fairly verbose. So yeah, this isn't the shortest program ever (especially with the included boilerplate). But it's recursive and takes an input and does what we need it to do.

For a much less verbose way of performing this function (as someone who certainly doesn't know all the languages out there), I'd write a Lisp function:

defun calculate (n)
  (if (or (zerop n) (= n 1))
    (+ (calculate (- n 1)) (calculate (- n 2)))))

In the REPL, (calculate 5) here will do the same thing as entering 5 when the Java code above is compiled.

But I'm not actually here to talk about the Fibonacci sequence. I'm not really here to talk about verbosity or recursion either. Right now, I'm here to talk about natural language programming (and, in particular, Inform 7).

If you haven't heard of Inform 7 (which is not unlikely, to be honest), it is a programming language designed for writing Interactive Fiction—you know, Adventure, Zork, Jigsaw (I'm going to use this as a plug to say if you have not played 1995's IF Jigsaw, do yourself a favor and go through it). Yes, it's a language designed for reacting to (mostly) fairly simple commands—"go west", "take sword", "kill bugbear".

But the language fascinated me immediately. Inform 7 is known as a programming language that is closer to natural language than any other. The language itself is designed to read as a story, as a narrative—even though the users of the software developed in this language don't ever see the code it was programmed in. It's a language built specifically for the writer of the code; a way, likely, to let the author mix logic and narrative without breaking a prosaic tone any more than necessary.

I wasn't sure how the language would function when I first started experimenting with it. When I started to read through the documentation (which, to be honest, is quite convoluted and difficult to navigate), I wasn't sure if it would be capable of what other high-level languages could do. So I wanted to test it. And I decided that my first test should involve writing a recursive Fibonacci function. It was a lot of fun.

Before I get into it, I will give a brief preface. First, this language is not meant to replace a high-performing language like Java, and it isn't optimized to run even medium-weight programs. Second, if you thought Java was verbose... well just wait. And third, seriously, this language is not meant for heavy-duty programming. But just the same, it's incredibly interesting.

Here is the Inform 7 code that mimics the above recursive Fibonacci functions:

"Fib" by G. Ryan Spain

The Lab is a room.

The fibonacci number is a number which varies. The fibonacci number is 1.

Calculating is an action applying to one number.

Understand "calculate [number]" as calculating.

To decide what number is the fibonacci index of (the index - number) (this is fibbing):
    if the index is greater than one:
        let the first number be fibbing applied to the index minus one;
        let the second number be fibbing applied to the index minus two;
        decide on the first number plus the second number;
        decide on 1;

Carry out calculating:
    let the answer be the number understood;
    Now the fibonacci number is fibbing applied to the answer;

Report calculating:
    say "[fibonacci number]";

Wasn't that crazy readable? It's pretty likely that many of you could read the Java code at the beginning of the article without much of a problem. The Lisp was probably readable by fewer of you—if you don't know Lisp or a Lisp dialect already, or if you haven't done much functional programming, it might take longer to read the Lisp and its seemingly indefinite parentheses.

And yes, reading the Inform code isn't quite like talking to a friend on the phone, or in person (I know because I have recently read some of my Inform code out loud to friends), but it's likely more readable to the passer-by—who has never seen your code before—than Lisp's odd-looking functions or even Java's somewhat-convoluted classes/methods/variables/what-have-you.

I hope to be able to look further into this language and consider it both from a writing and a programming perspective. I think that Inform brings up some interesting questions and hypotheticals that deserve consideration. In the meantime, I'd love to get your thoughts on Inform 7, or on general natural vs. artificial language topics. Let me know what you think of it in the comments.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

natural language,programming,inform 7,lisp,java

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}