Wednesday, January 22, 2003
      ( 4:12 PM ) Matt  

A new language

Every computer programmer comes up with at least one idea for a new language. These are invariably new computer languages, and not natural languages. My latest idea for a new language is a lazy, functional, postfix language. When talking about computer languages, lazy doesn't mean that it is hard to motivate, and functional doesn't mean that it isn't broken. A lazy language is one which only evaluates an expression when the value is needed. A functional language is one which always gives the same answer if you don't change the parameters. If you didn't understand that last bit. I'm not going to test you on it, but it is explained below.

I need a name for my language. How about Pomella? My friend Kenneth just mentioned something called that, and that's a cool word. Oh, wait, he said Promela. I wonder if Pomella is a word. Wow. I just did a Google search and found that Pomella is the genus for some species of snails. That seems pretty appropriate for any computer language I could implement.

Pomella is a postfix language. This means that the operators come after the operands. This contrasts with an infix or prefix language. For example, most arithmetic notation is infix, the operator comes between the operands: 1 + 2 means: add one and two. Lisp is a prefix language, the operator comes before the operators: (+ 1 2). PostScript (yes, the language that many printers use) is a postfix language: 1 2 +.

Pomella is a functional language. Math functions are functional. That's where the name came from. For example, sqrt(9) is always 3. Now, you might be thinking, of course it's always 3. When would you ever want it to be something aside from 3? Well, I agree with you. That's why Pomella is functional.

Pomella is lazy. This means it doesn't do any work it doesn't have to do. You might ask, "Why would that matter? Maybe it would go a little faster, but wouldn't the results be the same?" That's a good question. Let me give you an example.

First, let's define a few new operators. '=' takes two operands and if they are equal, the value is true, otherwise it is false. '?' takes 3 operands. If the last one is true, the value is the first one, and if it is false, then the value is the second one. 'print' takes 1 operand and prints it to the display and the value is that of the operand. Now, using those operands, what do you think this program does:

"Hello, " print "Bill." print "World!" print 0 1 = ? +

Well, I'm sure you've guessed it. It prints what everone's first program should: "Hello, World!" First, let's look at what it would do if Pomella were not lazy. Starting from the left, we have the value "Hello, ", the operator print would take this and print it to the screen, leaving the value there. Simillarly, "Bill." would also be printed, as would "World!". The numbers 0 and 1 would be compared, resulting in a false value, and then the question mark would select the second value which is "World!". Finally the two remaining values on the stack, "Hello, " and "World!" would be added together. So, in the end "Hello, Bill.World!" would be printed to the display, and the final value would be "Hello, World!"

Because Pomella is lazy, it would never evaluate the "Bill." Print part because that value is not needed to come up with the final answer. Now, you're probably saying, but the answer is the same. Why do we care how it got it? Well, in a perfect world, the way we get an answer shouldn't matter, but if in the process of getting the answer you have to do something that has external consequences, then it does matter how you got there. These external consequences are called side-effects. I'd love to have a language with no side-effects, but that's actually pretty difficult. Consider the print command above. Printing something to the display is a side-effect. It's hard to get very far if you can't communicate with the user.

So, that's just a taste... Maybe more later... I never did finish that "gaze detection" post.

# -

Comments: Post a Comment

Dreams I have...

Powered by Blogger
Feel free to e-mail me.

free hit counter